我有以下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": "<>"
}
]
},
Run Code Online (Sandbox Code Playgroud)
等等
我的最终目标是递归地跟踪这个树,用<ul>标签包装每个键/对象名称,以及某种<form>结构中"ParticleIndex"级别的属性,但我无法弄清楚如何索引这两个'$ values '阵列.
这是我一直在操作以学习如何访问每个元素(对象或数组)的代码:
foreach ($json->NBBList->'$values'[0] as $key => $value){
var_dump($key);
echo "\n".var_dump($value);
echo "\n\n\n";
}
Run Code Online (Sandbox Code Playgroud)
这显然不起作用,因为值的索引在字符串之外,但是当它在内部时,PHP将其解释为字符串的一部分.
有没有办法让我能够索引'$ values'数组的每个元素,并最终在for循环中?
我在想使用JSON解码的"真实"属性可能是一个更好的解决方案......
您可以使用以下表示法访问名称包含特殊字符的对象属性:
$json->NBBList->{'$values'}[0]
Run Code Online (Sandbox Code Playgroud)
我不认为这种行为在任何地方都有记录,但你可以在PHP语法中找到它(参见使用的定义variable_name,在object_dim_list其中使用object_property).
| 归档时间: |
|
| 查看次数: |
627 次 |
| 最近记录: |