JsonConvert.DeserializeObject array issue



我有这个JSON,我正在尝试反序列化:

{
  "events": [
    {
      "eventName": "service-review-created",
      "version": "1",
      "eventData": {
        "id": "XXXXXXbca843690a3015d3c0",
        "language": "en",
        "stars": 5,
        "title": "I was particularly impressed by the...",
        "text": "I was particularly impressed by the...",
        "referenceId": null,
        "createdAt": "2019-03-29T23:05:32Z",
        "link": "https://api.trustpilot.com/v1/reviews/XXX",
        "consumer": {
          "id": "XXXXXX40000ff000a74b9e1",
          "name": "XXX",
          "link": "https://api.trustpilot.com/v1/consumers/5899b3140000ff000a74b9e1"
        }
      }
    }
  ]
}

这些是我的模型:

public class Event
{
    public string eventName { get; set; }
    public string version { get; set; }
    public EventData eventData { get; set; }
}
public class EventData
{
    public string id { get; set; }
    public string language { get; set; }
    public int stars { get; set; }
    public string title { get; set; }
    public string text { get; set; }
    public string referenceId { get; set; }
    public DateTime createdAt { get; set; }
    public string link { get; set; }
    public Consumer consumer { get; set; }
}
public class Consumer
{
    public string id { get; set; }
    public string name { get; set; }
    public string link { get; set; }
}

当我尝试时:

List<Event> events = JsonConvert.DeserializeObject<List<Event>>(jsonstring);

我得到:

无法反序列化当前 JSON 对象(例如 { "名称": "值" } ( 转换为类型"System.Collections.Generic.List'1[LambdaFeedFunctions.Trustpilot.Models.Event]",因为该类型需要一个 JSON 数组(例如 [1,2,3](才能正确反序列化。 若要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3](或更改反序列化类型,使其是可以从 JSON 对象反序列化的普通 .NET 类型(例如,不是整数等基元类型,也不是数组或列表等集合类型(。还可以将 JsonObjectAttribute 添加到类型中,以强制它从 JSON 对象反序列化。

我不确定我需要更改什么才能使其工作。

使用 Json2CSharp:

public class Consumer
{
    public string id { get; set; }
    public string name { get; set; }
    public string link { get; set; }
}
public class EventData
{
    public string id { get; set; }
    public string language { get; set; }
    public int stars { get; set; }
    public string title { get; set; }
    public string text { get; set; }
    public string referenceId { get; set; }
    public DateTime createdAt { get; set; }
    public string link { get; set; }
    public Consumer consumer { get; set; }
}
public class Event
{
    public string eventName { get; set; }
    public string version { get; set; }
    public EventData eventData { get; set; }
}
public class RootObject
{
    public List<Event> events { get; set; }
}

然后

RootObject rootEvents = JsonConvert.DeserializeObject<RootObject>(jsonstring);

现在,您可以通过以下方式访问您的活动:

rootEvents.events

您的 JSON 有一个 RootObject ,这将需要另一个包装器 RootObject 类才能正确反序列化,如前所述。

或者,如果您可以更改 JSON 结构以发送数组(不带 RootObject(,如下所示,它应该可以解决。

[
  {
    "eventName": "service-review-created",
    "version": "1",
    "eventData": {
      "id": "XXXXXXbca843690a3015d3c0",
      "language": "en",
      "stars": 5,
      "title": "I was particularly impressed by the...",
      "text": "I was particularly impressed by the...",
      "referenceId": null,
      "createdAt": "2019-03-29T23:05:32Z",
      "link": "https://api.trustpilot.com/v1/reviews/XXX",
      "consumer": {
        "id": "XXXXXX40000ff000a74b9e1",
        "name": "XXX",
        "link": "https://api.trustpilot.com/v1/consumers/5899b3140000ff000a74b9e1"
      }
    }
  }
]

相关内容

  • 没有找到相关文章

最新更新