我有以下JSON文件作为输入,
{
"$type": "NanoWebInterpreter.WebInputData, NanoWebInterpreter",
"NBBList": {
"$type": "System.Collections.Generic.List`1[[monoTNP.Common.NBB, monoTNP.Common]], mscorlib",
"$values": [
{
"$type": "monoTNP.Common.NBB, monoTNP.Common",
"ID": "id-0065-00000003",
"MPList": {
"$type": "System.Collections.Generic.List`1[[monoTNP.Common.MP, monoTNP.Common]], mscorlib",
"$values": [
{
"$type": "monoTNP.Common.EllipticalMP, monoTNP.Common",
"Eccentricity": 1.0,
"ID": "id-0065-00000006",
"ParticleIndex": -1,
"DispersionInteractionStrength": 0.0,
"DispersionInteractionRange": 2.5,
"CharacteristicSize": 0.0,
"CenterOfMass": "<0,0,0>",
"OrientationVector": "<>"
},
{
"$type": "monoTNP.Common.CubeMP, monoTNP.Common",
"ID": "id-0065-00000005",
"ParticleIndex": -1,
"DispersionInteractionStrength": 0.0,
"DispersionInteractionRange": 2.5,
"CharacteristicSize": 0.0,
"CenterOfMass": "<0,0,0>",
"OrientationVector": "<>"
},
{
"$type": "monoTNP.Common.CircularMP, monoTNP.Common",
"ID": "id-0065-00000004",
"ParticleIndex": -1,
"DispersionInteractionStrength": 0.0,
"DispersionInteractionRange": 2.5,
"CharacteristicSize": 0.0,
"CenterOfMass": "<0,0,0>",
"OrientationVector": "<>"
}
]
},
我的最终目标是递归地追踪这个树形结构,用<ul>
标签包裹每个键/对象名称,并将“ParticleIndex”级别的属性放入某种<form>
结构中,但我无法理解如何索引这两个“$values”数组。
这是我一直在操作以了解如何访问每个元素(对象或数组)的代码:
foreach ($json->NBBList->'$values'[0] as $key => $value){
var_dump($key);
echo "\n".var_dump($value);
echo "\n\n\n";
}
显然这样做不起作用,因为值的索引超出了字符串范围,但当它在内部时,PHP将其解释为字符串的一部分。
有没有办法索引到'$values'数组中的每个元素,并最终在for循环中使用?
我认为使用JSON decode的"true"属性可能是更好的解决方案...