我有一个如下的json响应
{
"Robert": [
{
"id": "123",
"class": "7th",
"lastname": "johnson"
}
],
"William": [
{
"id": "124",
"class": "7th",
"lastname": "parker"
}
]
}
我想将其映射到c#类。使用在线转换器,我得到以下结构
public class Robert {
public string id { get; set; }
public string class { get; set; }
public string lastname { get; set; }
}
public class William {
public string id { get; set; }
public string class { get; set; }
public string lastname { get; set; }
}
public class Application {
public IList<Robert> Robert { get; set; }
public IList<William> William { get; set; }
}
相反,我想将学生姓名映射为班级属性之一,例如..“姓名”
public class Student{
public string Name{ get; set; } //Robert is mapped to this
public string id { get; set; }
public string class { get; set; }
public string lastname { get; set; }
}
我该怎么做?
您可以像这样在JSON之上阅读
var responseObject = JsonConvert.DeserializeObject<JObject>(responseStream); //Deserialized JSON to type of JObject
var robert = responseObject["Robert"].ToObject<Student>();
您的学生班级应该是这样
public class Student{
public string id { get; set; }
public string class { get; set; }
public string lastname { get; set; }
}
由此,您可以读取Robert的ID,类,姓氏。
只是读取您的JSON响应的选项。