1

any way to loop in a json file and print all keys and values like a dictionary?

Example

foreach (string item in result.Data.Keys)
   {
      Debug.LogError("KEY:"+item);
      Debug.LogError("Value:" + result.Data[item]);
   }

I have tried JsonUtility and simple json, but i cant print the KEY value yet

Any solution? thanks

Community
  • 1
  • 1
Gustavo Otero
  • 91
  • 2
  • 11

2 Answers2

1

You could try using FullSerializer instead for your JSON files which is a bit more powerful than the standard JSON Utility. It is available from https://github.com/jacobdufault/fullserializer.

In this case you can use the fsData.AsDictionary to convert it to a regular dictionary.

fsData data = fsJsonParser.Parse(serializedString);
// do something with `data.AsDictionary`

Then you would iterate over the resulting Dictionary as normal.

will-hart
  • 3,742
  • 2
  • 38
  • 48
1

The built-in JsonUtility class is limited to deserializing into plain classes and structs. Deserialization into dictionaries does not seem to be supported at this time. LitJSON is quite a bit more flexible.

Using LitJSON:

var deserializedObject = JsonMapper.ToObject(json_text);
foreach(var key in deserializedObject.Keys) 
{
    var value = deserializedObject[key]
}
ow3n
  • 5,974
  • 4
  • 53
  • 51
Jephron
  • 2,652
  • 1
  • 23
  • 34