我有这个json字符串
{
'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
{}
JSONSerializer.ConvertFromJSON<root>(JSONData)
我想反序列化上面的json字符串但是得到空值?知道哪里不对吗?
public static T ConvertFromJSON<T>(String json)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Deserialize<T>(json);
}
答案 0 :(得分:5)
尝试更改您的课程:
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; }
}
你的JSON有错误
'gender':male,
应该是
'gender': 'male',