需要以下JSON响应的帮助。我试着反序列化
JSON响应
{
"entities":[
{
"id":"/Severity/Critical",
"Name":"Critical"
},
{
"id":"/Severity/High",
"Name":"High"
},
{
"id":"/Severity/Low",
"Name":"Low"
},
{
"id":"/Severity/Normal",
"Name":"Normal"
}
],
"paging":{
"from":4,
"limit":100,
"hasMore":false
}
}
var severity = JsonConvert.DeserializeObject<Severity>(json);
错误消息
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.String]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
要修复此错误,请将JSON更改为JSON对象(例如{“name”:“value”})或将反序列化类型更改为数组或实现集合接口的类型(例如ICollection,IList)可以从JSON数组反序列化的列表。 JsonArrayAttribute也可以添加到类型中,以强制它从JSON数组反序列化。
答案 0 :(得分:0)
正如ErrorMessage告诉你的那样,JsonConvert.DeserializeObject无法将您用作输入的JSON转换为对象。为避免这种情况,您可以声明一个特定的类,在该实例中,您的JSON将被反序列化。根据您分享的输入,此类可能如下所示:
public class Severity
{
[JsonProperty("entities")]
public List<Entity> Entities { get; set; }
[JsonProperty("paging")]
public Paging Paging { get; set; }
}
public class Entity
{
[JsonProperty("id")]
public string Id;
[JsonProperty("Name")]
public string Name;
}
public class Paging
{
[JsonProperty("from")]
public int From { get; set; }
[JsonProperty("limit")]
public int Limit { get; set; }
[JsonProperty("hasMore")]
public bool HasMore { get; set; }
}
然后你可以这样做:
var severity = JsonConvert.DeserializeObject<Severity>(json);