这是我的JSON文件,叫做(inventory.json)。如何解析 json,以便我可以在"描述"数组中获取清单的所有tag_name?由于库存数组的 classid 指向描述的 id/classid,因此似乎可以从描述中获取每个库存的标签,但我不知道该怎么做。
我读了一些关于递归数组迭代器的内容,但我不知道在这种情况下哪一个是合适的。我是新来的,所以请善待!谢谢。
{
"Inventory": {
"7905269096": {
"id": "7905269096",
"classid": "771158876",
"instanceid": "782509058",
"amount": "1",
"pos": 1
},
"7832200468": {
"id": "7832200468",
"classid": "626495772",
"instanceid": "1463199080",
"amount": "1",
"pos": 2
},
"7832199378": {
"id": "7832199378",
"classid": "626495770",
"instanceid": "1463199082",
"amount": "1",
"pos": 3
},
"Descriptions": {
"771158876": {
"classid": "771158876",
"instanceid": "782509058",
"tags": [{
"tag_name": "unique",
"name": "standard"
}]
}
}
}
}
您的 JSON 字符串无效,但希望这个答案能引导您走上通往所需结果的正确道路。
首先,将其制作成PHP
数组:
$jsonArray = /* your json array */;
$phpArray = json_decode($jsonArray, true);
然后,您可以遍历其中一个数组("库存"数组)并找到相关的标签名称:
//Create a new array to hold the key=>value data
$combined = [];
foreach($phpArray["Inventory"] as $i => $v){
//Find the relevant key in the Descriptions array and
//push this information information to the $combined array
$combined[$i] = $phpArray["Descriptions"][$i]["tags"]["tag_name"];
/* The above is essentially the same as
$combined["7905269096"] = "unique"; */
}
然后,$combined
将是一个键/值数组,其中键是ID(例如"7905269096"),值将是标签名称(例如"唯一")。