Php 解析具有未知字段名的JSON数组?
我有一个json:Php 解析具有未知字段名的JSON数组?,php,json,parsing,Php,Json,Parsing,我有一个json: { "success": true, "rgInventory": { "4580331488": { "id": "4580331488", "classid": "520025252", "instanceid": "0", "amount": "1", "pos": 1 }, "4566197481": { "id": "4566197481", "cla
{
"success": true,
"rgInventory": {
"4580331488": {
"id": "4580331488",
"classid": "520025252",
"instanceid": "0",
"amount": "1",
"pos": 1
},
"4566197481": {
"id": "4566197481",
"classid": "1439482117",
"instanceid": "188530139",
"amount": "1",
"pos": 2
},
"4566196610": {
"id": "4566196610",
"classid": "1439484944",
"instanceid": "188530139",
"amount": "1",
"pos": 3
},
"4566097797": {
"id": "4566097797",
"classid": "310776859",
"instanceid": "302028390",
"amount": "1",
" pos": 4
},
"4565915026": {
"id": "4565915026",
"classid": "310776840",
"instanceid": "302028390",
"amount": "1",
" pos": 5
},
"4565415060": {
"id": "4565415060",
"classid": "1439489161",
"instanceid": "188530139",
"amount": "1",
"pos": 6
},
"4565413906": {
"id": "4565413906",
"classid": "1439484334",
"instanceid": "188530139",
"amount": "1",
"pos": 7
},
"4559798755": {
"id": "4559798755",
"classid": "520025252",
"instanceid": "0",
"amount": "1",
"pos": 8
},
"4553954652": {
"id": "4553954652",
"classid": "520025252",
"instanceid": "0",
"amount": "1",
"pos": 9
},
"4553694029": {
"id": "4553694029",
"classid": "991959905",
"instanceid": "0",
"amount": "1",
"pos": 10
},
"4553694015": {
"id": "4553694015",
"classid": "720289133",
"instanceid": "188530170",
"amount": "1",
" pos": 11
},
"4553694006": {
"id": "4553694006",
"classid": "1364486740",
"instanceid": "188530139",
"amount": "1",
"pos": 12
},
"4553693992": {
"id": "4553693992",
"classid": "1309991239",
"instanceid": "188530139",
"amount": "1",
"pos": 13
},
"4553693986": {
"id": "4553693986",
"classid": "310776711",
"instanceid": "302028390",
"amount": "1",
" pos": 14
}
}
我需要将其作为数组获取并解析它。问题是,这个json来自我的清单,当我获取其他人的清单时,字段将发生变化,我不知道如何解析它。
到目前为止,我在这里:
$link='http://steamcommunity.com/profiles/'.$steamid.'/inventory/json/730/2';
$string = file_get_contents($link);
$obj = json_decode($string, true);
var_dump($obj['rgInventory']);
在这一点上,我可以得到一个结果,但我不知道如何超越这一点,我的意思是,我需要返回每个记录的classid和实例id,但是hw可以在JSON中这样做吗?您可以使用
foreach
循环
foreach ($obj['rgInventory'] as $key => $item) {
echo $key; // echo the "unknown" field name
echo $item['classid']; // echo any field value
}
具体哪一个领域会改变?rgInventory会有不同的键,还是classId等的名称会改变?是的,键会改变