【发布时间】:2019-06-21 22:40:41
【问题描述】:
我有一个这样的 json:
[
{
\"childNodes\":null,
\"children\":null,
\"key\":\"\",
\"subKey\":{
\"buffer\":\"\",
\"offset\":0,
\"length\":0,
\"value\":\"\",
\"hasValue\":true
},
\"isContainerNode\":false,
\"rawValue\":null,
\"attemptedValue\":null,
\"errors\":[
{
\"exception\":null,
\"errorMessage\":\"Incorrect password.\"
}
],
\"validationState\":1
}
]
所以我想把它反序列化成模型,所以我创建了这样的模型:
public class JsonDeserializeModel
{
public class SubKey
{
public string buffer { get; set; }
public int offset { get; set; }
public int length { get; set; }
public string value { get; set; }
public bool hasValue { get; set; }
}
public class Error
{
public object exception { get; set; }
public string errorMessage { get; set; }
}
public class RootObject
{
public object childNodes { get; set; }
public object children { get; set; }
public string key { get; set; }
public SubKey subKey { get; set; }
public bool isContainerNode { get; set; }
public object rawValue { get; set; }
public object attemptedValue { get; set; }
public List<Error> errors { get; set; }
public int validationState { get; set; }
}
}
然后我尝试反序列化为:
JsonDeserializeModel completeObject = JsonConvert.DeserializeObject<JsonDeserializeModel>(response.content);
但它会抛出异常:
Newtonsoft.Json.JsonSerializationException: '无法反序列化 当前 JSON 数组(例如 [1,2,3])转换为类型 'Project.Models.JsonDeserializeModel' 因为该类型需要 JSON 对象(例如 {"name":"value"})正确反序列化。修理 此错误要么将 JSON 更改为 JSON 对象(例如 {"name":"value"}) 或将反序列化类型更改为数组或 实现集合接口的类型(例如 ICollection、IList) 比如可以从 JSON 数组反序列化的 List。 JsonArrayAttribute 也可以添加到类型中以强制它 从 JSON 数组反序列化。路径'',第 1 行,位置 1。'
我不明白错误在哪里。有人可以知道或有更多经验知道什么是错的吗?问候
【问题讨论】:
-
JsonConvert.DeserializeObject<List<RootObject>>