无法将 JSON 对象反序列化为类型“System.Collections.Generic.List'1[obJ
本文关键字:List Generic obJ Collections System JSON 对象 反序列化 类型 | 更新日期: 2023-09-27 18:35:39
我正在尝试反序列化(使用Newtonsoft)JSON并在c#中转换为List。它抛出我错误"无法将 JSON 对象反序列化为类型'System.Collections.Generic.List'1[obJson]'。
这是我的 JSON 字符串:
string webContent = "{'"searchResults'": [{'"gefId'":0,'"resultNumber'":1,'"distance'":4.2839,'"sourceName'":'"MQA.MQ_34172_HD'",'"name'":'"USER_DEFINED'"},{'"gefId'":0,'"resultNumber'":1,'"distance'":4.2839,'"sourceName'":'"MQA.MQ_34172_HD'",'"name'":'"USER_DEFINED'"}]}";
转换,此行抛出错误:
List<obJson> result = JsonConvert.DeserializeObject<List<obJson>>(webContent);
我的自定义类:
public class SearchResults
{
public int gefId { get; set; }
public int resultNumber { get; set; }
public decimal distance { get; set; }
public string sourceName { get; set; }
public string name { get; set; }
}
public class obJson
{
public SearchResults SearchResults { get; set; }
}
由于 json 是一个对象,其searchResults
成员包含数组,因此请按如下方式更改obJson
public class obJson
{
public List<SearchResults> searchResults { get; set; }
}
并反序列化为
obJson result = JsonConvert.DeserializeObject<obJson>(webContent);
问题出在您的模型上,或者相反,与您发送的数据有关。您正在接收一个数组,并希望将其反序列化为普通对象。您可以像更改模型一样
public class obJson
{
public SearchResults[] SearchResults { get; set; }
}
您的结果将被反序列化就好了。
您的 JSON 无效。
Parse error on line 1:
{ '"searchResults'": [
-----^
Expecting 'STRING', '}'
http://jsonlint.com/