PHP使用json為何總是報錯

來源:互聯網
上載者:User
$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);
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.