- Home /
JSON Deserialization
Hi,
I'm still beginner in json deserialization.
I have this JSON I get.
{
"1": {
"id": 1,
"user_id": "xx",
"booth_type": "EXPERT",
"category": "Digital",
"virtual_location": "G-01-201",
"template": "B0167-3",
"payment_status": "PAID",
"status": null,
"date_registered": "30/10/2020",
"panels": {
"1": {
"id": 1,
"type": "youtubeVideo",
"mime_type": "youtubeVideo",
"url": xx"
},
"2": {
"id": 3,
"type": "banner",
"mime_type": "image/jpeg",
"url": "xx"
},
"3": {
"id": 4,
"type": "logo",
"mime_type": "image/png",
"url": "xx"
},
"4": {
"id": 5,
"type": "brochure",
"mime_type": "application/pdf",
"url": "xx"
}
}
},
"2": {
"id": 2,
"user_id": "yy",
"booth_type": "STANDARD",
"category": "DIGITAL",
"virtual_location": "G-01-202",
"template": "B0167-2",
"payment_status": "PENDING",
"status": null,
"date_registered": "30/10/2020",
"panels": {
"1": {
"id": 6,
"type": "poster",
"mime_type": "application/pdf",
"url": "yy"
},
"2": {
"id": 7,
"type": "logo",
"mime_type": "image/png",
"url": "yy"
},
"3": {
"id": 8,
"type": "photo",
"mime_type": "image/png",
"url": "yy"
}
}
}
}
How do I deserialize this json to object? is it possible with JSON.Utility?
Answer by Bunny83 · Nov 05, 2020 at 10:12 AM
No, Unity's JsonUtility can not deserialize your json object as your objects contain invalid field names in C#. While Json allows any string to be used as key in an object, when you map this object to a C# object you have to create a class with those key names as variable names. Since your key names are "0" and "1" those can not be variable names.
If you are still interested in mapping your json to actual C# objects you can use the Json.Net library and let it map those objects to a dictionary.
However if you just want to access / parse the data you could use my SimpleJSON parser. It does not map your javascript objects to C# objects but just parses them into a custom tree structure which makes it easy to work with the data they contain.
JSONNode root = JSON.Parse(yourJsonText);
string type = root["1"]["booth_type"]; // reads "EXPERT"
JSONNode panel = root["1"]["panels"]["1"];
string pType = panel["type"]; // reads "youtubeVideo"
string pUrl = panel["url"]; // reads "xx"
// to iterate through all panels of the second object
foreach(var kv in root["2"]["panels"])
{
int id = kv.Value["id"];
string type = kv.Value["type"];
string url = kv.Value["url"];
// ...
}
Note that if you are in control of generating those json objects, you really should use arrays instead of objects. Arrays have to start with an index of 0 to be recognised as an array by most serializers (thinking about php or javascript). Arrays also must have continuous indices. Since your "keys" seem to just enumerate the objects and panels those probably should be arrays instead.
When they are actual Json arrays, Unity's JsonUtility would be back on the table. Though it has a small limitation. The first / root element has to be an object and can not be an array. So you would need to wrap your array in an object first. So your json would need to look like this:
{
"data" : [
{
"id": 1,
"user_id": "xx",
"booth_type": "EXPERT",
"category": "Digital",
"virtual_location": "G-01-201",
"template": "B0167-3",
"payment_status": "PAID",
"status": null,
"date_registered": "30/10/2020",
"panels": [
{
"id": 1,
"type": "youtubeVideo",
"mime_type": "youtubeVideo",
"url": xx"
},
{
"id": 3,
// ....
}
],
}
]
}
If you can change your json to something like that you can use the JsonUtility with those classes:
[System.Serializable]
public class JsonResponse
{
public List<DataObject> data;
}
[System.Serializable]
public class DataObject
{
public int id;
public string user_id;
public string booth_type;
public string category;
public string virtual_location;
public string template;
public string payment_status;
public string status;
public string date_registered;
public List<Panel> panels;
}
[System.Serializable]
public class Panel
{
public int id;
public string type;
public string mime_type;
public string url;
}
and to deserialize it you just do
JsonResponse response = JsonUtility.FromJson<JsonResponse>(yourJsonText);
Alright, the best solution is the redo the json again right? Thank you for elaborate answer
Answer by swanne · Nov 05, 2020 at 09:19 AM
Hi,
I recommend you research and gain a good understanding of the theory behind serializing and deserializing JSON. When you understand how it works, using Unity's JSON.Utility is very simple.
There are some good videos on YouTube which explain working with JSON and the theory is the same across all platforms. When you've done that, check out the unity docs -https://docs.unity3d.com/ScriptReference/JsonUtility.FromJson.html
Essentially, you need to create a class which defines the object you want to create then deserialize the JSON string to create an object and populate the attributes of the class.
Thank you. I watched the tutorial but I can't get my head wrap around this json. I tried many ways but couldn't make it work.
Your answer
Follow this Question
Related Questions
Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer
ArgumentException: Cannot deserialize JSON to new instances of type 0 Answers
Json.net deserialization problem 1 Answer
Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers
JSON deserialization 0 Answers