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)
It worked friend. But I didn’t understand why then c# have created Rootobject. you could tell me.
– Wallace Ferreira
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.– ramaral