PHP JSON Decoding: Array with $ Problem

I have the following JSON file as input,

{ "$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": "<>" } ] }, 

and etc.

My ultimate goal is to trace this tree recursively, wrapping each key / object name with <ul> tags and properties at the "ParticleIndex" level in some kind of <form> structure, but I canโ€™t figure out how to index $ arrays into arrays.

This is the code I manipulated to find out how each element (object or array) is accessed:

 foreach ($json->NBBList->'$values'[0] as $key => $value){ var_dump($key); echo "\n".var_dump($value); echo "\n\n\n"; } 

This obviously does not work, because the index of values โ€‹โ€‹is outside the line, but when it is inside, PHP interprets it as part of the line.

Is there any way for me to index each element of the $ values โ€‹โ€‹array and, ultimately, in a for loop?

I think using a true JSON decoding property might be a better solution ...

+6
source share
4 answers

You can access the properties of an object with names that contain special characters using these notations:

 $json->NBBList->{'$values'}[0] 

I don't think this behavior is documented anywhere, but you can find it in the PHP grammar (see the definition of variable_name , which is used in object_dim_list , which is used in object_property ).

+6
source

Set assoc json_decode to false to get arrays (dictionaries) instead of objects:

 $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"; } 
+4
source
 foreach($json->NBBList->{'$values'}[0] as $key=>$value){ 

You can use curly braces around the string to access the properties of the object with special characters.

+1
source

Have you tried something like?

 $show_values = $values[0]; foreach ($json->NBBList->'$show_values' as $key => $value){ var_dump($key); echo "\n".var_dump($value); echo "\n\n\n"; 

Just an idea, I'm not sure how much it will work.

-2
source

Source: https://habr.com/ru/post/890245/


All Articles