如何反序列化 json 对 .net 对象的响应
how to deserialize json response to .net object
responsestring = await response.Content.ReadAsStringAsync();
ClassifierResponse Response = JsonConvert.DeserializeObject<ClassifierResponse>(responsestring);
我的json回复是
{
"resultList": [
{
"modelId": 11,
"modelName": "indves12",
"modelLang": "US",
"modelVersion": 5,
"scoreMap": {
"individual": 0.401956,
"vessel": 0.598043
},
"bestCategory": "vessel"
}
]
}
我收到此错误:
Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[System.String]',因为该类型需要一个 JSON 数组(例如 [ 1,2,3]) 正确反序列化。
要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是原始类型像整数,而不是像数组或列表这样的集合类型),可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。
试试这个 class
public class ClassifierResponse
{
public List<ResultList> ResultList { get; set; }
}
public class ResultList
{
public int ModelId {get; set;}
public string ModelName { get; set; }
public string ModelLang { get; set; }
public int ModelVersion { get; set; }
public Scoremap Scoremap { get; set;}
public string BestCategory { get; set; }
}
public class Scoremap
{
public double Individual{ get; set; }
public double Vessel { get; set; }
}
responsestring = await response.Content.ReadAsStringAsync();
ClassifierResponse Response = JsonConvert.DeserializeObject<ClassifierResponse>(responsestring);
我的json回复是
{
"resultList": [
{
"modelId": 11,
"modelName": "indves12",
"modelLang": "US",
"modelVersion": 5,
"scoreMap": {
"individual": 0.401956,
"vessel": 0.598043
},
"bestCategory": "vessel"
}
]
}
我收到此错误: Newtonsoft.Json.JsonSerializationException: '无法将当前 JSON 对象(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1[System.String]',因为该类型需要一个 JSON 数组(例如 [ 1,2,3]) 正确反序列化。 要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是原始类型像整数,而不是像数组或列表这样的集合类型),可以从 JSON 对象反序列化。 JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。
试试这个 class
public class ClassifierResponse
{
public List<ResultList> ResultList { get; set; }
}
public class ResultList
{
public int ModelId {get; set;}
public string ModelName { get; set; }
public string ModelLang { get; set; }
public int ModelVersion { get; set; }
public Scoremap Scoremap { get; set;}
public string BestCategory { get; set; }
}
public class Scoremap
{
public double Individual{ get; set; }
public double Vessel { get; set; }
}