gpt4 book ai didi

c# - 反序列化嵌套的 json 字符串

转载 作者:行者123 更新时间:2023-11-30 15:07:29 25 4
gpt4 key购买 nike

我有这个 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);
}

json

最佳答案

试着像这样改变你的类:

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',

关于c# - 反序列化嵌套的 json 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6533124/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com