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?
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);
- 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_.name = test*["name"].Value;*_
userList_.description = test*[“name”].Value;
userList.type = test["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(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.
}
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);