使用PHP解析JSON并显示内容

时间:2012-06-10 01:32:33

标签: php html json

我遇到此代码的问题

这是我的PHP与其他项目合作......

$gas = file_get_contents('http://api.mygasfeed.com/stations/loadbygeo/47.9494949/120.23423432/reg|mid|pre|diesel/'. $api . '.json?callback=?');



$json_output = json_decode(utf8_decode($gas));

$location= $json_output->geoLocation->city_id;

这是JSON结果

?({"status":{"error":"NO","code":200,"description":"none","message":"Request    ok"},"geoLocation":{"city_id":"13123","city_long":"Hulunber","region_short":"Nei Mongol","region_long":"Nei Mongol","country_long":"China","country_id":"49","region_id":"6010"},"stations":[]})

此代码返回空白结果。

1 个答案:

答案 0 :(得分:0)

您需要删除?()。请参阅WP:JSONP

可以使用substr(),或者更好的是使用正则表达式来断言和删除所需的垃圾:

$json = preg_replace("/ ^[?\w(]+ | [)]+\s*$ /x", "", $jsonp);

会删除任意callbackFnNames(,还会删除您的奇怪?(伪函数调用。

相关问题