PHP 原生的函数库支持解析 JSON 数据为 PHP 数组
json_decode($data,$boolean);
这其中 data 参数是 json 的文本数据,大概能实现这样的转换效果:
{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5
}
[
"one",
"two",
"three"
]
[
{
"attr": "string",
"attr_num": 1,
"boolean": true
},
{
"attr": "string",
"attr_num": 1,
"boolean": true
}
]
Array(
'a'=>1,
'b'=>2,
'c'=>3,
'd'=>4,
'e'=>5
)
Array(
'1'=>'one',
'2'=>'two',
'3'=>'three'
)
Array(
[
'attr' => 'string',
'attr_num' => 1,
'boolean' => true,
],
[
'attr' => 'string',
'attr_num' => 1,
'boolean' => true,
],
)
大概写个例子
$data=json_decode($json,true);
echo $data[0][attr];//echo 'string';
读取并解析远程 json 文件
//require CURL
function GetCurl($url){
$curl = curl_init();
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl,CURLOPT_URL, $url);
curl_setopt($curl,CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
//get and parse data
$resp = GetCurl($data);
$resp = json_decode($resp,true);