[英]JSON PHP Decode Array and Objects
There are so many JSON/PHP/Decode related post out there... but I am struggling here. 那里有很多与JSON / PHP / Decode相关的帖子...但是我在这里苦苦挣扎。 I'm pretty sure I have built my JSON incorrectly, but I can't seem to see my mistake. 我敢肯定我没有正确构建JSON,但似乎看不到我的错误。 Can anyone help? 有人可以帮忙吗?
{
"badging": [
{
"event": [
{
"eventName": "Covent Garden",
"numberOfRooms": "1",
"mainLang": "xx",
"timeZone": "saas"
}
]
},
{
"names": [
{
"id": "1",
"fName": "Daniel",
"pos": "King"
},
{
"id": "2",
"fName": "Dasha",
"pos": "Queen"
}
]
}
]
}
This is posted to a PHP page and json_decode is used 这将发布到PHP页面,并使用json_decode
$json = $_POST['feed'];
$list = json_decode($json, TRUE);
And I feel I should be able to access the data like: 我觉得我应该能够像这样访问数据:
echo $list[1][2]['fName'];
or 要么
echo $list->badging[1]->names[2]->fName;
but I can't seem to do it. 但我似乎做不到。 thanks 谢谢
It should be: 它应该是:
print($list["badging"][1]["names"][0]["fName"]); // outputs "Daniel"
print($list["badging"][1]["names"][1]["fName"]); // outputs "Dasha"
Try $list['badging'][1]['names'][1]['fName'];
尝试$list['badging'][1]['names'][1]['fName'];
http://codepad.org/IdneHeDL http://codepad.org/IdneHeDL
Also if you omit the true to json_decode then $list->badging[1]->names[1]->fName;
另外,如果您省略json_decode的true,则$list->badging[1]->names[1]->fName;
badging $list->badging[1]->names[1]->fName;
http://codepad.org/WXWkVqo1 http://codepad.org/WXWkVqo1
This is how your array looks. 这就是数组的外观。
array(1) {
["badging"]=>
array(2) {
[0]=>
array(1) {
["event"]=>
array(1) {
[0]=>
array(4) {
["eventName"]=>
string(13) "Covent Garden"
["numberOfRooms"]=>
string(1) "1"
["mainLang"]=>
string(2) "xx"
["timeZone"]=>
string(4) "saas"
}
}
}
[1]=>
array(1) {
["names"]=>
array(2) {
[0]=>
array(3) {
["id"]=>
string(1) "1"
["fName"]=>
string(6) "Daniel"
["pos"]=>
string(4) "King"
}
[1]=>
array(3) {
["id"]=>
string(1) "2"
["fName"]=>
string(5) "Dasha"
["pos"]=>
string(5) "Queen"
}
}
}
}
}
eg. 例如。 echo $list['badging'][0]['event'][0]['eventName']; echo $ list ['badging'] [0] ['event'] [0] ['eventName'];
As per your JSON, by JSON_decode, we get 根据您的JSON,通过JSON_decode,我们得到
Array ( [badging] => Array ( [0] => Array ( [event] => Array ( [0] => Array ( [eventName] => Covent Garden [numberOfRooms] => 1 [mainLang] => xx [timeZone] => saas ) ) ) [1] => Array ( [names] => Array ( [0] => Array ( [id] => 1 [fName] => Daniel [pos] => King ) [1] => Array ( [id] => 2 [fName] => Dasha [pos] => Queen ) ) ) ) )
So, to access it, you need to it use 因此,要访问它,您需要使用
echo $list['badging'][1]['names'][0]['fName'];
Happy Coding :) 快乐编码:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.