php提取json数组里面的值怎么提?

$filename = "http://whois.pconline.com.cn/ipJson.jsp?json=true";
$json = json_decode(file_get_contents($filename));
$city=$json->city;
我这个提不出来

先把获取的内容打印出来 ,看看是不是正常返回的数据

$filename = "http://whois.pconline.com.cn/ipJson.jsp?json=true";
$content = file_get_contents($filename);

$json = @json_decode($content );
if($json){
    $city=$json->city;
}else{
    echo "json解析失败:".$content;
}

也可以根据你的框架功能,写成日志,方便出错时随时检查 

另外 ,json_decode 可以接收一个参数来确定解析成对象还是数组

$content = '{"ip":"120.239.177.231","pro":"广东省","proCode":"440000","city":"中山市","cityCode":"442000","region":"","regionCode":"0","addr":"广东省中山市 移通","regionNames":"","err":""}';
$json = json_decode($content,true);
echo $json['city'];

参考文档:PHP json_decode

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-04-05
json_decode需要是UTF8编码得,你检查下file_get_content下来得内容是不是gbk得,或者在decode前使用iconv把内容字符转下utf8的,如果还是解析不出来得话,json_get_erroer有这么个函数,可以去获取下解析失败得原因
相似回答