复制代码 代码如下:
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] }
复制代码 代码如下:
<?php
//php中用数组表示JSON格式数据
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', '非诚'),
'lastname' => iconv('gb2312', 'utf-8', '勿扰'),
'contact' => array(
'email' =>'fcwr@vevb.com',
'website' =>'http://www.vevb.com',
)
);
//将数组编码成JSON数据格式
$json_string = json_encode($arr);
//JSON格式数据可直接输出
echo $json_string;
?>
复制代码 代码如下:
<?php
//php中用数组表示JSON格式数据
$arr = array(
'firstname' => iconv('gb2312', 'utf-8', '非诚'),
'lastname' => iconv('gb2312', 'utf-8', '勿扰'),
'contact' => array(
'email' =>'fcwr@vevb.com',
'website' =>'http://www.vevb.com',
)
);
//将数组编码成JSON数据格式
$json_string = json_encode($arr);
//将JSON格式数据进行解码,解码后不是JSON数据格式,不可用echo直接输出
$obj = json_decode($json_string);
//强制转化为数组格式
$arr = (array) $obj;
//按数组方式调用里面的数据
echo iconv('utf-8','gb2312',$arr['firstname']);
echo '</br>';
//输出数组结构
print_r($arr);
?>
新闻热点
疑难解答