deserialize a nested json string
i have this json string
{
'studentinfo':
{
'name':'abc',
'age':41,
'gender':male,
address:
{
'street':'asd',
'city':'ipd',
'state':'mah'
},
'subject':[
{
'name':'Arts','marks':40,'grade':'a'
},
{
'name':'Science','marks':40,'grade':'a'
},
{
'name':'Commerce','marks':40,'grade':'a'
}
]
}
}
class root
{
public list<studentinfo> studentinfo;
public list<address> address;
public list<subject> subject;
}
class studentinfo
{}
class address
{}
class subject
{}
JSONSerialize开发者_StackOverflowr.ConvertFromJSON<root>(JSONData)
i want to deserialize the above json string but am getting null values? Any idea where it is incorrect?
public static T ConvertFromJSON<T>(String json)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<T>(json);
}
Try to change your classes like this:
class root
{
public root()
{
}
public studentinfo studentinfo { get; set; }
}
class studentinfo
{
public studentinfo()
{
subject = new List<subject>();
}
public string name { get; set; }
public int age { get; set; }
public string gender { get; set; }
public address address;
public List<subject> subject;
}
class address
{
public string street { get; set; }
public string city { get; set; }
public string state { get; set; }
}
class subject
{
public string name { get; set; }
public int marks { get; set; }
public string grade { get; set; }
}
and there's an error with your JSON
'gender':male,
should be
'gender': 'male',
精彩评论