Unity Community


Results 1 to 7 of 7

  1. Posts
    9

    C# deserialize JSON array

    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/json...ty3d/downloads as well as the MiniJSON script from here: https://raw.github.com/gist/1411710/MiniJSON.cs

    Here's my test code

    Code:  
    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. Posts
    31
    You should try to deserialize an array instead of a list... Something like this..

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


  3. 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. Posts
    31
    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. Posts
    9
    Found the problem. If I remove the setter from the Person class then it works just fine.

    Code:  
    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;
    10.     public int Age;
    11. }
    12.  
    13. public class test : MonoBehaviour {
    14.     void Start() {
    15.         string json = "[{\"Name\":\"Fred\",\"Age\":5},{\"Name\":\"Molly\",\"Age\":13}]";
    16.  
    17.         Person[] jsonObject = JsonReader.Deserialize<Person[]>(json);
    18.         foreach(var row in jsonObject)
    19.             Debug.Log(row.Name);
    20.     }
    21. }

    results in
    Fred
    Molly

    Thanks for the help


  6. Location
    Portland, OR
    Posts
    228
    For future Googlers, the problem could also be fixed by adding a constructor with no parameters

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


  7. Location
    Beijing
    Posts
    48
    Thanks guys!!!
    You really save my whole night!!!
    108km Studio
    --- Game: Robot Bros. - A 3D Action+Puzzle Game!! iTune Store
    --- Game: Soccer Fighter IS OUT! - 3D soccer game. Click To iTune Store
    --- -
    --- Game: Devil Golf IS OUT! Click To iTune Store

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •