I have two class:
[System.Serializable]
public class oneTask{
public string id;
public string username;
public string task;
public string category;
public string time;
public bool status;
}
public class taskcollection{
public oneTask[] tasksjson;
}
And my conversion is
taskcollection temptasks = JsonUtility.FromJson<taskcollection> (jsonString);
But it will come with an Error: ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson[taskcollection] (System.String json)
Could anyone help with how to convert string to JSON array in Unity? The JSON is like:
[{"id":"1","username":"Hanslen","task":"Writing PEC cw2","category":"Study","time":"2016-11-18 00:00:00","status":false},{"id":"2","username":"Hanslen","task":"Prepare for MLE presentation","category":"Study","time":"2016-11-24 00:00:00","status":false},{"id":"3","username":"Hanslen","task":"Some testing task1","category":"Life","time":"2017-02-20 22:09:52","status":false},{"id":"4","username":"Hanslen","task":"Some testing task2","category":"Movie","time":"2017-02-20 22:09:52","status":true},{"id":"5","username":"Hanslen","task":"Some testing task3","category":"Life","time":"2017-02-20 22:09:55","status":false},{"id":"6","username":"Hanslen","task":"Some testing task4","category":"Shopping","time":"2017-02-20 22:09:55","status":false},{"id":"7","username":"Hanslen","task":"server","category":"test","time":"2017-02-21 17:38:32","status":true},{"id":"8","username":"Hanslen","task":"Website task adding","category":"Study","time":"2017-02-21 17:40:58","status":false},{"id":"9","username":"Hanslen","task":"Another website task adding","category":"undefined","time":"2017-02-21 17:53:01","status":false},{"id":"10","username":"Hanslen","task":"adasdasdsa","category":"Life","time":"2017-02-21 17:53:18","status":false}]
EDIT:
The Json from server does not contain anything about taskcollection
class. It only has info about the oneTask
class. You have to do JsonHelper.FromJson<oneTask>(jsonString);
not JsonHelper.FromJson<taskcollection>(jsonString);
You have to fix the Json then use JsonHelper
to deserialize it.
string fixJson(string value)
{
value = "{\"Items\":" + value + "}";
return value;
}
//Fix Json
jsonString = fixJson(jsonString);
//Deserialize it
oneTask[] temptasks = JsonHelper.FromJson<oneTask>(jsonString);