PHP JSON解码:带有'$';问题
我有以下JSON文件作为输入PHP JSON解码:带有'$';问题,php,json,Php,Json,我有以下JSON文件作为输入 { "$type": "NanoWebInterpreter.WebInputData, NanoWebInterpreter", "NBBList": { "$type": "System.Collections.Generic.List`1[[monoTNP.Common.NBB, monoTNP.Common]], mscorlib", "$values": [ { "$type": "monoTNP.Com
{
"$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": "<>"
}
]
},
这显然不起作用,因为值的索引在字符串外部,但当它在内部时,PHP将其解释为字符串的一部分
我是否有办法索引到“$values”数组的每个元素,并最终在for循环中
我认为使用JSON decode的“true”属性可能是一个更好的解决方案…将的
assoc
参数设置为false,以获取数组(字典)而不是对象:
$json = json_decode($jsonInput, true);
foreach ($json['NBBList']['$values'][0] as $key => $value){
var_dump($key);
echo "\n";
var_dump($value);
echo "\n\n\n";
}
你试过类似的东西吗
$show_values = $values[0];
foreach ($json->NBBList->'$show_values' as $key => $value){
var_dump($key);
echo "\n".var_dump($value);
echo "\n\n\n";
只是一个想法,我不确定它会有多好的效果
foreach($json->NBBList->{'$values'}[0] as $key=>$value){
您可以在字符串周围使用大括号来访问具有特殊字符的对象的属性。您可以使用以下符号访问具有包含特殊字符的名称的对象属性:
$json->NBBList->{'$values'}[0]
我不认为这种行为在任何地方都有文档记录,但您可以在中找到它(请参阅
变量名称的定义,它在对象维度列表中使用,该列表在对象属性中使用).我为您格式化了您的帖子。json仍然无效,无论它是否已格式化。可能的重复:您必须用{'}
包装奇数名称,即使您想对其进行迭代。感谢您的指针,在另一篇帖子中看到json_解码字符串提醒了我它的价值,有一个例子是这样的:啊,好的。我已经在OOP部分查找了文档。谁会想到他们已经把它放到了JSON文档中^^这个问题被问了很多,所以一个例子(比如SimpleXML)是有意义的
$json->NBBList->{'$values'}[0]