JsonUtility.FromJson Create List<Object> [duplicate] JsonUtility.FromJson Create List<Object> [duplicate] json json

JsonUtility.FromJson Create List<Object> [duplicate]


Unity's new Json API does not support Json array and that is exactly what your problem is. There is a way to do it but it is a long process to repost again. Just read how to do it here.What you are looking for is the second solution that says "2. MULTIPLE DATA(ARRAY JSON)."


Creating a class and another class holding list of it works for me.

[Serializable]public class Questions{    public string id;    public string question;    public string answer1;    public string answer2;}[Serializable]public class QuestionList{    public List<Questions> list;}//andvar jstring = "{\"list\":[{\"id\":\"1\",\"question\":\"lorem Ipsome \",\"answer1\":\"yes\",\"answer2\":\"no\"},{\"id\":\"2\",\"question\":\"lorem Ipsome Sit dore iman\",\"answer1\":\"si\",\"answer2\":\"ne\"}]}";var result = JsonUtility.FromJson<QuestionList>(jstring);

From a list, Convert to JSON

List<Questions> questionlist = new List<Questions>();questionlist.Add(new Questions(){    answer1 = "yes",    answer2 = "no",    id = "1",    question = "lorem Ipsome "});questionlist.Add(new Questions(){    answer1 = "si",    answer2 = "ne",    id = "2",    question = "lorem Ipsome Sit dore iman"});var ql = new QuestionList();ql.list = questionlist;var result = JsonUtility.ToJson(ql);  //this gives{"list":[{"id":"1","question":"lorem Ipsome ","answer1":"yes","answer2":"no"},{"id":"2","question":"lorem Ipsome Sit dore iman","answer1":"si","answer2":"ne"}]}

Note: "list" and questionList.list should be in same name.