简体   繁体   中英

Get all json keys and values in c# (Unity)

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

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.

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]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM