Json Multidimensional for Object in C #

0

I'm learning C # and am having some difficulty passing a multidimensional json to an object doing a deserialize. I'm not sure how to resolve this.

See my json

string json2 = @"[
                            {
                                        firstName: ""joao"",
                                        lastName: ""silva"",
                                        dateOfBirth:
                                                     {
                                                        year: ""1990"",
                                                        month: ""01"",
                                                        day: ""01""
                                                     }
                            },
                            {
                                        firstName: ""carla"",
                                        lastName: ""dias"",
                                        dateOfBirth:
                                                     {
                                                        year: ""2000"",
                                                        month: ""02"",
                                                        day: ""02""
                                                    }
                            }
                         ]";

Now the classes I created (actually it was C # with special paste)

  public class Class1
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public Dateofbirth dateOfBirth { get; set; }
    }


    public class Dateofbirth
    {
        public string year { get; set; }
        public string month { get; set; }
        public string day { get; set; }
    }

    public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

Now I'm trying to do deserialize

 List<Rootobject> listLad = JsonConvert.DeserializeObject<List<Rootobject>>(json2);

Response.Write(listLad[0].Property1[0].lastName);

But it gives error (I'm very accustomed to php)

    
asked by anonymous 31.08.2016 / 21:05

2 answers

2

You've been there close. It only needs the Class1 and Dateofbirth classes.

What this json represents is a list of objects that can be represented by Class1 , so the Rootobject class p>

Desserialize like this:

List<Class1> listLad = JsonConvert.DeserializeObject<List<Class1>>(json2);

Note:

I do not know why Visual Studio generates the other class, you might consider that json, to be well formatted, should look like this:

{
    Property1:[
        {
                    firstName: ""joao"",
                    lastName: ""silva"",
                    dateOfBirth:
                                 {
                                    year: ""1990"",
                                    month: ""01"",
                                    day: ""01""
                                 }
        },
        {
                    firstName: ""carla"",
                    lastName: ""dias"",
                    dateOfBirth:
                                 {
                                    year: ""2000"",
                                    month: ""02"",
                                    day: ""02""
                                }
        }
    ]
}
    
31.08.2016 / 23:04
-1

Use fastJSON [^] ' s Parse ( ) method that will give you Dictionary <string , object> JSON representation that you can traverse.

    
31.08.2016 / 21:47