我試圖將JSON信息放入一個PHP變量中,並且我總是得到錯誤。如何將JSON放入PHP變量中?
這是我的代碼,以使JSON可讀:
header('Content-Type: application/json');
$jsondecode1 = json_decode($json1);
print_r ($jsondecode1);
這是$jsondecode1
的一個片段:
Array
(
[0] => stdClass Object
(
[flightId] => ******
[******] => stdClass Object
(
[******] => ******
[******] => ******
[******] => ******
[******] => ******
[******] => ******
)
注意:當我echo
的jsondecode1
它輸出這樣的:
陣列到字符串轉換 C:\ XAMPP ...... \ simpletest3.php上 線
所以我用print_r()
。
我試圖把(例如)[flightId]
到一個PHP變量:
$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1->flightId;
print_r ($jsondecode2);
輸出:注意:試圖讓財產下不發對象的 'FLIGHTID' :. ........ \在線simpletest3.php
我嘗試了一些其他的代碼也一樣,但產出非常相似,我不想讓我的問題比需要的時間更長。 那麼,如何將(例如)[flightId]
放入PHP變量中。
編輯:
解決方案:
$jsondecode1 = json_decode($json1);
$jsondecode2 = $jsondecode1[0]->flightId;
print_r ($jsondecode2);
或者:
foreach ($jsondecode1 as $data) {
print_r($data->flightId);
}
大它也能工作。非常感謝你。 :) – Deadpool
@duffbeerisgood當你使用'json_decode()'它將json字符串轉換成對象數組 –
@duffbeerisgood所以我應該使用json_decode()嗎? – Deadpool