废话就不多说,直接上代码。
<?php
echo phpversion(),"\n";
$json='{"code":"200","location":[{"name":"深圳","id":"101280601","lat":"22.54700","lon":"114.08594","adm2":"深圳","adm1":"广东省","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"13","fxLink":"http://hfx.link/3i71"}],"refer":{"sources":["QWeather"],"license":["commercial license"]}}';
echo $json,"\n";
$jsonobj=json_decode($json);
#["location"]
echo var_dump($jsonobj),"\n";
#["fxLink"]
$location= $jsonobj->location[0];
echo $location->fxLink,"\n";
?>
结果:
7.4.1{"code":"200","location":[{"name":"深圳","id":"101280601","lat":"22.54700","lon":"114.08594","adm2":"深圳","adm1":"广东省","country":"中国","tz":"Asia/Shanghai","utcOffset":"+08:00","isDst":"0","type":"city","rank":"13","fxLink":"http://hfx.link/3i71"}],"refer":{"sources":["QWeather"],"license":["commercial license"]}}object(stdClass)#1 (3) {
["code"]=>
string(3) "200"
["location"]=>
array(1) {
[0]=>
object(stdClass)#2 (13) {
["name"]=>
string(6) "深圳"
["id"]=>
string(9) "101280601"
["lat"]=>
string(8) "22.54700"
["lon"]=>
string(9) "114.08594"
["adm2"]=>
string(6) "深圳"
["adm1"]=>
string(9) "广东省"
["country"]=>
string(6) "中国"
["tz"]=>
string(13) "Asia/Shanghai"
["utcOffset"]=>
string(6) "+08:00"
["isDst"]=>
string(1) "0"
["type"]=>
string(4) "city"
["rank"]=>
string(2) "13"
["fxLink"]=>
string(20) "http://hfx.link/3i71"
}
}
["refer"]=>
object(stdClass)#3 (2) {
["sources"]=>
array(1) {
[0]=>
string(8) "QWeather"
}
["license"]=>
array(1) {
[0]=>
string(18) "commercial license"
}
}
}
http://hfx.link/3i71