那里有很多JSON / PHP / Decode相关帖子......但我在这里苦苦挣扎。我很确定我错误地构建了我的JSON,但我似乎无法看到我的错误。有人可以帮忙吗?
{
"badging": [
{
"event": [
{
"eventName": "Covent Garden",
"numberOfRooms": "1",
"mainLang": "xx",
"timeZone": "saas"
}
]
},
{
"names": [
{
"id": "1",
"fName": "Daniel",
"pos": "King"
},
{
"id": "2",
"fName": "Dasha",
"pos": "Queen"
}
]
}
]
}
这是发布到PHP页面并使用json_decode
$json = $_POST['feed'];
$list = json_decode($json, TRUE);
我觉得我应该可以访问以下数据:
echo $list[1][2]['fName'];
或
echo $list->badging[1]->names[2]->fName;
但我似乎无法做到。 感谢
答案 0 :(得分:1)
应该是:
print($list["badging"][1]["names"][0]["fName"]); // outputs "Daniel"
print($list["badging"][1]["names"][1]["fName"]); // outputs "Dasha"
答案 1 :(得分:1)
尝试$list['badging'][1]['names'][1]['fName'];
http://codepad.org/IdneHeDL
此外,如果省略json_decode的true,则$list->badging[1]->names[1]->fName;
http://codepad.org/WXWkVqo1
答案 2 :(得分:1)
这就是你的数组的外观。
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"
}
}
}
}
}
例如。 echo $ list ['badging'] [0] ['event'] [0] ['eventName'];
答案 3 :(得分:0)
根据您的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 ) ) ) ) )
因此,要访问它,您需要使用
echo $list['badging'][1]['names'][0]['fName'];
快乐编码:)