JsonUtility not working
Hello, i use this code:
string input = @"{""first"": ""Foo"", ""last"": ""Bar""}";
var res = JsonUtility.FromJson<Dictionary<string, object>> (input)
But variable "res" is empty dictionary. What i do wrong ? The same situation i have when i use this code:
Dictionary<string, object> tmpDict1 = new Dictionary<string, object> ();
tmpDict1.Add("first", "Foo");
Dictionary<string, object> tmpDict2 = new Dictionary<string, object> ();
tmpDict2.Add("second", "Joe");
List<Dictionary<string, object>> lst = new List<Dictionary<string, object>> ();
lst.Add (tmpDict1);
lst.Add (tmpDict2);
var res = JsonUtility.ToJson(lst);
The variable "res" is empty string like "{}". What is it ? Why is it ?
Answer by zeh · Dec 12, 2015 at 06:32 PM
Simply put, Unity does not support serialization and deserialization of dictionaries. As it is, JsonUtility
is useless unless you want straight class serialization/deserialization from/to models that already exist.
If your data is in a dictionary, you can serialize it if you convert to arrays of keys and values first. But then the JSON won't be following the same schema.
I hope they'll support actual Dictionaries soon. You may want to watch this thread or vote for this request.
Your answer
Follow this Question
Related Questions
Projectiles not shooting correctly 1 Answer
1 script for multiple enemy 1 Answer
Ambient Emitter inside a BoxCollider - Rotation not recognized 0 Answers
Timer reset on object pickup script? 1 Answer