$filename = "http://whois.pconline.com.cn/ipJson.jsp?json=true";$json = json_decode(file_get_contents($filename));$city=$json->city;我這個提不出來
2 回答

青春有我
TA貢獻(xiàn)1784條經(jīng)驗 獲得超8個贊
先把獲取的內(nèi)容打印出來 ,看看是不是正常返回的數(shù)據(jù)
$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解析失?。?quot; . $content ; } |
也可以根據(jù)你的框架功能,寫成日志,方便出錯時隨時檢查
另外 ,json_decode 可以接收一個參數(shù)來確定解析成對象還是數(shù)組
$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' ]; |

泛舟湖上清波郎朗
TA貢獻(xiàn)1818條經(jīng)驗 獲得超3個贊
json_decode需要是UTF8編碼得,你檢查下file_get_content下來得內(nèi)容是不是gbk得,或者在decode前使用iconv把內(nèi)容字符轉(zhuǎn)下utf8的,如果還是解析不出來得話,json_get_erroer有這么個函數(shù),可以去獲取下解析失敗得原因
添加回答
舉報
0/150
提交
取消