- Home /
How do I go about deserializing a json array?
I'm currently making a Task system for my game and I want task data to be stored within a json file like so:
[
{
"name": "Task one",
"description": "This is task one",
"type": 0
},
{
"name": "Task two",
"description": "This is task two",
"type": 2
},
{
"name": "Task three",
"description": "This is task three",
"type": 1
}
]
Although it doesn't seem like Unity has any way of handling json arrays. How do I do this?
Answer by Arcana96 · Dec 27, 2016 at 05:58 PM
Here's a helper class that does it all for you.
public class JsonHelper
{
public static T[] getJsonArray<T>(string json)
{
string newJson = "{ \"array\": " + json + "}";
Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(newJson);
return wrapper.array;
}
[System.Serializable]
private class Wrapper<T>
{
public T[] array;
}
}
Then you can just do something like the following:
YourClass[] yourClass; yourClass = JsonHelper.getJsonArray(Your Json Data);
I realize this is a little off topic but just out of curiosity, is there a document size limit on son files?
There's not an inherent size, no. However, some servers limit the amount of Json data that can be sent.
Thanks @Arcana96, Reason I ask is because we are using a tyson file for our Voice Recognition inputs, I seem to have reached a point in the document where if I try and add more voice commands the file no longer works.... and that was why I was wondering if there was a size limit on the document itself?
Can you please send your scripts here so i can see how you get the JSON file so.
Answer by Jeet_9811 · Jan 19, 2018 at 06:46 PM
With SimpleJson In the web call back function we will deserialize the response Json into an array or list.
static void WebCall (string jsonString) { Debug.Log ("Response :" + jsonString);
JSONNode test = JSONNode.Parse (jsonString);
int count = test.Childs.Count ();
List<User> userList = new List<User>();
for (int i = 0; i<test.Childs.Count(); i++) {
userList[i].name = test[i]["name"].Value;
userList[i].description = test[i]["name"].Value;
userList[i].type = test[i]["name”].AsInt;
}
}
[System.Serializable] public class User {
public string name;
public string description;
public int type;
}
2 And simply with JsonUtility you can do
static void WebCall(){
string response = "{\"users\": [\ n {\ n \ "name\ ": \ "Task one\ ",\ n \ "description\ ": \ "This is task one\ ",\ n \ "type\ ": 0\ n },\ n {\ n \ "name\ ": \ "Task two\ ",\ n \ "description\ ": \ "This is task two\ ",\ n \ "type\ ": 2\ n },\ n {\ n \ "name\ ": \ "Task three\ ",\ n \ "description\ ": \ "This is task three\ ",\ n \ "type\ ": 1\ n }\ n ]}";
Debug.Log ("Response :" + response);
UserList userLst = JsonUtility.FromJson<UserList>(response);
foreach(User user in userLst){
Debug.Log(“User data: "+ user.name + " " + user.description + “ “ + user.type.ToString());
}
[System.Serializable]
public class User {
public string name;
public string description;
public int type;
}
[System.Serializable]
public class UserList {
public User [] users; //Note: users - should be the key for your array in json string.
}
Answer by CaptainIsolation · Feb 16 at 08:41 PM
If anyone come's here with JSON data that is deserialising but an inner array of objects is not deserialising, the [Serializable] tag on the inner object was all that I had missed:
JSON:
{
"Name": "name",
"InnerObjects":[
{
"Value1":"1",
"Value2":"2"
},
{
"Value1":"3",
"Value2":"4"
}
]
}
C# Model:
public class ObjectFromJSON
{
public string Name;
public InnerObject[] InnerObjects;
}
[Serializable] //This attribute includes it in the JSONUtility deserialisation
public class InnerObject
{
public string Value1;
public string Value2;
}
C# Unity JSONUtility deserialisation
ObjectFromJSON obj = JsonUtility.FromJson<ObjectFromJSON>(jsonMetadataString);