【问题标题】:C# Deserialize JSON to List [duplicate]C#反序列化JSON到列表[重复]
【发布时间】:2018-02-11 10:01:04
【问题描述】:

我在尝试将 JSON 字符串(包含多个对象)反序列化为列表时遇到了一个我不明白的问题。这是 JSON。

[  
   {  
      "id":2,
      "name":"Race 1",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   },
   {  
      "id":3,
      "name":"Race 2",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   },
   {  
      "id":4,
      "name":"Race 3",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   },
   {  
      "id":5,
      "name":"Race 4",
      "raceDateTime":"2017-09-02T14:27:39.654",
      "raceStartTime":"2017-09-02T14:27:39.654",
      "description":"string",
      "maxEntries":0,
      "currentEntries":0,
      "status":0
   }
]

然后我的 JSON 参数与我的模型匹配。

public class RaceModel
{

    public int id { get; set; }
    public string name { get; set; }
    public DateTime raceDateTime { get; set; }
    public DateTime raceStartTime { get; set; }
    public string description { get; set; }
    public int maxEntries { get; set; }
    public int currentEntries { get; set; }
    public int status { get; set; }

}

public class RaceList
{
    public List<RaceList> racelist { get; set; }
}

我从 REST API 请求中获取 JSON 的代码如下:

string APIServer = Application.Current.Properties["APIServer"].ToString();
string Token = Application.Current.Properties["Token"].ToString();
var client = new RestClient(APIServer);
var request = new RestRequest("api/race", Method.GET);
request.AddHeader("Content-type", "application/json");
request.AddHeader("Authorization", "Bearer " + Token);
var response = client.Execute(request) as RestResponse;

var raceobject = JsonConvert.DeserializeObject<RaceList>(response.Content);

但我收到此错误(使用 Newtonsoft.JSON)

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TechsportiseApp.API.Models.RaceList' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.

我想要一个对象列表/集合,然后我可以迭代和使用。

谁能告诉我我做错了什么?

【问题讨论】:

  • 你应该在 RaceList 类中使用 RaceModel 吗?
  • 好的,谢谢!

标签: c# json json.net


【解决方案1】:

您的响应是一个对象数组,并且您在 T 参数中指定了一个对象。使用List&lt;RaceModel&gt; 而不是RaceList

var raceobject = JsonConvert.DeserializeObject<List<RaceModel>>(response.Content);

【讨论】:

  • 谢谢,成功了!
  • @Matthew Warr 嗨,我有一个类似的 JSOn,我有 JsonConvert.DeserializeObject>(json);但我仍然得到一个类似问题的问题。我究竟做错了什么。示例包含一个 List 并且 Item 根据 JSON 有 5 个属性。
猜你喜欢
  • 1970-01-01
  • 2016-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多