我想反序列化下面的Json对象,
var jsonObject = {
"name": "sections",
"record": [
{
"id": 1,
"name": "sections",
"tables":
{
"sections":
{
"id": "1",
"type": "2"
}
}
}
]
}
在C#中
var result = JsonConvert.DeserializeObject<Response>(jsonObject);
为反序列化添加了以下类
public class Response
{
[JsonProperty("name")]
public string Name;
[JsonProperty("record")]
public List<Records> Record;
}
public class Records
{
[JsonProperty("id")]
public int Id;
[JsonProperty("name")]
public string Name;
[JsonProperty("tables")]
public List<Table> Tables;
}
public class Table
{
[JsonProperty("sections")]
public List<Sections> Sections;
}
public class Sections
{
[JsonProperty("id")]
public string id;
[JsonProperty("type")]
public string Type;
}
我想从json获取“Type”,但它没有正确反序列化。任何人都可以建议如何从Json对象中获取Type。
答案 0 :(得分:1)
从问题来看,课程并不匹配。
public class Response
{
public string Name;
public List<Records> Record;
}
public class Records
{
public int Id;
public string Name;
public List<Table> Tables;
}
public class Table
{
public List<Sections> Sections;
}
public class Sections
{
public string id;
public string Type;
}
章节没有[],表格也没有,所以它们不是列表。
我也将您的反序列化代码更改为此
var result = JsonConvert.DeserializeObject<Response>(jsonObject, new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
因此,您不能仅为驼峰式JSON注释每个类属性。
答案 1 :(得分:0)
由于属性type
未正确格式化,因此无法序列化对象
而不是
type ": "
2 "
您必须设置type
,如下所示
"type":"2"