Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# deserialize JSON array

Discussion in 'Scripting' started by xindexer, Dec 29, 2011.

  1. xindexer

    xindexer

    Joined:
    Dec 29, 2011
    Posts:
    9
    I have been fighting this one for a bit now, can't get the type casting right. I can deserialize a single JSON line but when I try to do an array, I'm running into problems. I have tried both the JSONFX code from here: https://bitbucket.org/darktable/jsonfx-for-unity3d/downloads as well as the MiniJSON script from here: https://raw.github.com/gist/1411710/MiniJSON.cs

    Here's my test code

    Code (csharp):
    1. using System;
    2. using System.Text;
    3. using System.Reflection;
    4. using System.Collections;
    5. using System.Collections.Generic;
    6. using JsonFX.Json;
    7.  
    8. public class Person {
    9.     public string Name { get; set; }
    10.     public int Age { get; set; }
    11.     public Person ( string name, int age) {
    12.         Name = name;
    13.         Age = age;
    14.     }
    15. }
    16.  
    17. public class test : MonoBehaviour {
    18.     void Start() {
    19.         Person person1 = new Person("Fred",5);
    20.         Person person2 = new Person("Molly",13);
    21.         object[] t = new object[2];
    22.         t[0] = person1;
    23.         t[1] = person2;
    24.  
    25.         string json = JsonWriter.Serialize(t);
    26.         // gives me the right string - [{"Name":"Fred","Age":5},{"Name":"Molly","Age":13}]
    27.  
    28.         List<Person> jsonObject = JsonReader.Deserialize<List<Person>>(json);
    29.         //fails - Only objects with default constructors can be deserialized
    30.     }
    31. }
    I'm missing something simple here - what is it?

    Thanks
     
  2. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    You should try to deserialize an array instead of a list... Something like this..

    Code (csharp):
    1.  
    2. Person[] jsonObject = JsonReader.Deserialize<Person[]>(json);
    3.  
     
  3. xindexer

    xindexer

    Joined:
    Dec 29, 2011
    Posts:
    9
    Person[] did not work but object[] did. This gives me a dictionary that I can parse which is great.

    One more question - is there a way to cast the result back to Person? would be nice to have it back as the original but this at least gets me moving forward again

    Thanks
     
  4. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    Not familiar with this JsonFX you are using, but the .Net equivalent would just work for Person[] as long as the original was also a Person[]

    Try changing

    object[] t = new object[2];

    to

    Person[] t = new Person[2];
     
  5. xindexer

    xindexer

    Joined:
    Dec 29, 2011
    Posts:
    9
    Found the problem. If I remove the setter from the Person class then it works just fine.

    Code (csharp):
    1.  
    2. using System;
    3. using System.Text;
    4. using System.Reflection;
    5. using System.Collections;
    6. using System.Collections.Generic;
    7. using JsonFX.Json;
    8.  
    9. public class Person {
    10.     public string Name;
    11.     public int Age;
    12. }
    13.  
    14. public class test : MonoBehaviour {
    15.     void Start() {
    16.         string json = "[{\"Name\":\"Fred\",\"Age\":5},{\"Name\":\"Molly\",\"Age\":13}]";
    17.  
    18.         Person[] jsonObject = JsonReader.Deserialize<Person[]>(json);
    19.         foreach(var row in jsonObject)
    20.             Debug.Log(row.Name);
    21.     }
    22. }
    23.  
    results in
    Fred
    Molly

    Thanks for the help
     
  6. Dark-Table

    Dark-Table

    Joined:
    Nov 25, 2008
    Posts:
    315
    For future Googlers, the problem could also be fixed by adding a constructor with no parameters

    Code (csharp):
    1.  
    2. public class Person {
    3.     public string Name { get; set; }
    4.     public int Age { get; set; }
    5.  
    6.     public Person() {
    7.         // this is the default constructor that JSONFX wanted.
    8.     }
    9.  
    10.     public Person ( string name, int age) {
    11.         Name = name;
    12.         Age = age;
    13.     }
    14. }
    15.  
     
  7. Waigo

    Waigo

    Joined:
    Jul 12, 2009
    Posts:
    49
    Thanks guys!!!
    You really save my whole night!!!