Multidimensional Json for Object in C#

Asked

Viewed 94 times

0

I’m learning c# and I’m having a hard time passing a multidimensional json to Object by doing an deserialize. I don’t really know how to fix it.

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 try to make the deserialize

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

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

but gives error (I’m very used to php)

2 answers

2


You walked right by.
Only needs the classes Class1 and Dateofbirth.

What is that json represents a list of objects that can be represented by Class1, so the class is not needed Rootobject

Desirialize like this:

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

Note:

I don’t know why Visual Studio generates the other class, perhaps consider that json, to be well formatted, should be 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""
                                }
        }
    ]
}
  • It worked friend. But I didn’t understand why then c# have created Rootobject. you could tell me.

  • I’m not sure, perhaps because, although no json is declared, there is a "Rootobject" (Classe1[]), or at least it should exist: json should look like this: {root:[ ... ]} If you use the json2csharp it only generates the two classes.

-1

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

  • From what I read fastJson he does the conversion to Dictionary without seeing a need for a class that represents that json. That’s it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.