$res = '{
"status": "ok", //介面狀態,參考http://www.heweather.com/documents/api
"basic": { //基本資料
"city": "北京", //城市名稱
"cnty": "中國", //國家
"id": "CN101010100", //城市ID,參見http://www.heweather.com/documents/cn-city-list
"lat": "39.904000", //城市維度
"lon": "116.391000", //城市經度
"update": { //更新時間
"loc": "2015-07-02 14:44", //當地時間
"utc": "2015-07-02 06:46" //UTC時間
}
}
}';
var_dump(json_decode($res)); //顯示結果
// $json = '{"foo": 12345}';
// $obj = json_decode($json);
// print $obj->{'foo'}; // 12345
?>
抱的錯都是類似syntax error, unexpected 'status' (T_STRING) in /Applications/MAMP/htdocs/testjson.php on line 5
回複討論(解決方案)
你的代碼就這些?那把json裡面注釋去掉。。。
你的代碼就這些?那把json裡面注釋去掉。。。
好像去掉注釋真的可以顯示出來了,大概是這樣的
object(stdClass)#1 (2)
{
["status"]=> string(2) "ok"
["basic"]=> object(stdClass)#2 (6)
{
["city"]=> string(6) "北京"
["cnty"]=> string(6) "中國"
["id"]=> string(11) "CN101010100"
["lat"]=> string(9) "39.904000"
["lon"]=> string(10) "116.391000"
["update"]=> object(stdClass)#3 (2)
{
["loc"]=> string(16) "2015-07-02 14:44"
["utc"]=> string(16) "2015-07-02 06:46"
}
}
}
排好版之後的樣子,那比如說 $res = json_decode($res) 之後應該怎麼提取這個數組裡面的“id”的值呢
$obj->basic->id
json_decode($json, true);
出來的就是數組了
好像不行,這樣沒法提取單個資料值
當我從一個URL擷取了json包之後,$res = curl_exec($ch); 不論怎麼處理,整個json包都會展現出來,而不是我想要提取的其中某個欄位
$ch = curl_init();
$url = 'https://api.heweather.com/x3/weather?cityid=CN101200101&key=7081f8010abe4638a86e0c4c1cfee30e';
// 執行HTTP請求
curl_setopt($ch , CURLOPT_URL , $url);
curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = json_decode(curl_exec($ch));
echo $res['now']['cond']['txt'];
?>
這段請求無法擷取到需要的天氣值
echo $res[key($res)][0]['now']['cond']['txt']; //多雲
其實你 print_r($res); 就可以看到 now 是在第三維的
注釋去掉即可
$res = '{ "status": "ok", "basic": { "city": "北京", "cnty": "中國", "id": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": { "loc": "2015-07-02 14:44", "utc": "2015-07-02 06:46" } }}';var_dump(json_decode($res)); //顯示結果
object(stdClass)[1]
public 'status' => string 'ok' (length=2)
public 'basic' =>
object(stdClass)[2]
public 'city' => string '北京' (length=6)
public 'cnty' => string '中國' (length=6)
public 'id' => string 'CN101010100' (length=11)
public 'lat' => string '39.904000' (length=9)
public 'lon' => string '116.391000' (length=10)
public 'update' =>
object(stdClass)[3]
public 'loc' => string '2015-07-02 14:44' (length=16)
public 'utc' => string '2015-07-02 06:46' (length=16)
擷取id可以這樣寫
$res = '{ "status": "ok", "basic": { "city": "北京", "cnty": "中國", "id": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": { "loc": "2015-07-02 14:44", "utc": "2015-07-02 06:46" } }}';$data = json_decode($res); //顯示結果echo $data->basic->id;
CN101010100
echo $res[key($res)][0]['now']['cond']['txt']; //多雲
其實你 print_r($res); 就可以看到 now 是在第三維的
好像還是不行,剛才試了一下你這個,也不能輸出任何東西
注釋去掉即可
$res = '{ "status": "ok", "basic": { "city": "北京", "cnty": "中國", "id": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": { "loc": "2015-07-02 14:44", "utc": "2015-07-02 06:46" } }}';var_dump(json_decode($res)); //顯示結果
object(stdClass)[1]
public 'status' => string 'ok' (length=2)
public 'basic' =>
object(stdClass)[2]
public 'city' => string '北京' (length=6)
public 'cnty' => string '中國' (length=6)
public 'id' => string 'CN101010100' (length=11)
public 'lat' => string '39.904000' (length=9)
public 'lon' => string '116.391000' (length=10)
public 'update' =>
object(stdClass)[3]
public 'loc' => string '2015-07-02 14:44' (length=16)
public 'utc' => string '2015-07-02 06:46' (length=16)
擷取id可以這樣寫
$res = '{ "status": "ok", "basic": { "city": "北京", "cnty": "中國", "id": "CN101010100", "lat": "39.904000", "lon": "116.391000", "update": { "loc": "2015-07-02 14:44", "utc": "2015-07-02 06:46" } }}';$data = json_decode($res); //顯示結果echo $data->basic->id;
CN101010100
麻煩看一下我7樓的代碼,從中如何提取天氣的txt
$ch = curl_init();$url = 'https://api.heweather.com/x3/weather?cityid=CN101200101&key=7081f8010abe4638a86e0c4c1cfee30e';// 執行HTTP請求curl_setopt($ch , CURLOPT_URL , $url);curl_setopt($ch , CURLOPT_SSL_VERIFYPEER , false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);$res = json_decode(curl_exec($ch), true); //json_decode 要有第二個參數,這樣可解析成數組echo $res[key($res)][0]['now']['cond']['txt'], PHP_EOL; //為什麼要這樣寫,看看 print_r 的輸出就知道了print_r($res);