PiggyChu001
PiggyChu001

Reputation: 440

How to read Json files properly in Unity?

I read this article and try it.

I wrote a very simple code to read the json file:

void Start()
{
    Debug.Log(evolution.text);
    try
    {
        EvolutionDic = JsonUtility.FromJson<Dictionary<string, string>>(evolution.text);
    }
    catch(Exception e)
    {
        Debug.Log(e.Message);
    }

    Debug.Log(EvolutionDic.Count);
}

The code read the evolution.text correctly, but didn't write into the dictionary.

I had tested the Json file in Visual C# and have no problem reading it into the dictionary.

Could somebody please be so kind and tell me where did I do wrong!?

Much appreciated!

Result Snapshot: enter image description here

Upvotes: 0

Views: 105

Answers (1)

Lotan
Lotan

Reputation: 4283

Dictionaries aren't natively serializable/deserializable.

  • But you can use Newtonsoft that I think it works with dictionaries, or any other third party serializer (which is probably what you do in your test Visual C# example).
  • Implement your own serialization/deserialization method for your structure.

Upvotes: 1

Related Questions