我解析了從另一個網站發佈的JSON,其中一個節點有一個孩子。Php json_decode解析讀取子值
$inputJSON = file_get_contents('php://input');
$input= json_decode($inputJSON); //convert JSON into object
$order_number = $input->{'order_no'};
$name = $input->{'name'};
$street_address = $input->{'address_1'};
$city =$input->{'city'};
$state = $input->{'region'} ;
$zip = $input->{'postal_code'};
我可以讀取所有的值。然而,該產品部分的格式
<items>
<product_code></product_code>
<product_name></product_name>
</items>
我想它讀成
$product_id = $input->{'items'}{'product_code'};
$product_description = $input->{'items'}{'product_name'};
但我得到我的變量沒有數據。什麼是正確的語法?
謝謝。
編輯:JSON輸出
object(stdClass)#1 (20) {
["test_order"]=>
string(1) "Y"
["shop_no"]=>
string(6) "142319"
["order_no"]=>
string(12) "TU5495467701"
string(5) "Smith"
["city"]=>
string(5) "Bosei"
["postal_code"]=>
string(6) "123456"
["order_total"]=>
string(5) "39.00"
["country"]=>
string(2) "HK"
["telephone"]=>
string(8) "12345678"
["pay_source"]=>
string(2) "CC"
["base_currency"]=>
string(3) "USD"
["items"]=>
array(1) {
[0]=>
object(stdClass)#2 (9) {
["product_price"]=>
string(5) "39.00"
["product_name"]=>
string(12) "Abcd Product"
["product_code"]=>
string(8) "142319-1"
}
}
["first_name"]=>
string(4) "John"
}
這是XML,而不是JSON。真正的JSON可能看起來也可能不看起來像這樣。 – deceze
添加了JSON輸出。 – Basu