JsonConvert.DeserializeObject 数组问题

我有这个 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 对象(例如 { "name": "value" } )反序列化为类型 'System.Collections.Generic.List`1[LambdaFeedFunctions.Trustpilot.Models.Event]' 因为该类型需要一个 JSON 数组(例如 [ 1,2,3]) 正确反序列化。要修复此错误,请将 JSON 更改为 JSON 数组(例如 [1,2,3])或更改反序列化类型,使其成为普通的 .NET 类型(例如,不是像整数这样的原始类型,也不是像这样的集合类型一个数组或列表),可以从 JSON 对象反序列化。JsonObjectAttribute 也可以添加到类型以强制它从 JSON 对象反序列化。


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


手掌心
浏览 228回答 2
2回答

FFIVE

使用Json2CSharp:public class Consumer{&nbsp; &nbsp; public string id { get; set; }&nbsp; &nbsp; public string name { get; set; }&nbsp; &nbsp; public string link { get; set; }}public class EventData{&nbsp; &nbsp; public string id { get; set; }&nbsp; &nbsp; public string language { get; set; }&nbsp; &nbsp; public int stars { get; set; }&nbsp; &nbsp; public string title { get; set; }&nbsp; &nbsp; public string text { get; set; }&nbsp; &nbsp; public string referenceId { get; set; }&nbsp; &nbsp; public DateTime createdAt { get; set; }&nbsp; &nbsp; public string link { get; set; }&nbsp; &nbsp; public Consumer consumer { get; set; }}public class Event{&nbsp; &nbsp; public string eventName { get; set; }&nbsp; &nbsp; public string version { get; set; }&nbsp; &nbsp; public EventData eventData { get; set; }}public class RootObject{&nbsp; &nbsp; public List<Event> events { get; set; }}然后RootObject rootEvents = JsonConvert.DeserializeObject<RootObject>(jsonstring);您现在可以通过以下方式访问您的活动:rootEvents.events

三国纷争

您的 JSON 有一个RootObject,这将需要另一个包装器 RootObject类才能如前所述正确反序列化。或者,如果您可以更改 JSON 结构以发送数组(不带RootObject),如下所示,它应该可以正常解析。[&nbsp; {&nbsp; &nbsp; "eventName": "service-review-created",&nbsp; &nbsp; "version": "1",&nbsp; &nbsp; "eventData": {&nbsp; &nbsp; &nbsp; "id": "XXXXXXbca843690a3015d3c0",&nbsp; &nbsp; &nbsp; "language": "en",&nbsp; &nbsp; &nbsp; "stars": 5,&nbsp; &nbsp; &nbsp; "title": "I was particularly impressed by the...",&nbsp; &nbsp; &nbsp; "text": "I was particularly impressed by the...",&nbsp; &nbsp; &nbsp; "referenceId": null,&nbsp; &nbsp; &nbsp; "createdAt": "2019-03-29T23:05:32Z",&nbsp; &nbsp; &nbsp; "link": "https://api.trustpilot.com/v1/reviews/XXX",&nbsp; &nbsp; &nbsp; "consumer": {&nbsp; &nbsp; &nbsp; &nbsp; "id": "XXXXXX40000ff000a74b9e1",&nbsp; &nbsp; &nbsp; &nbsp; "name": "XXX",&nbsp; &nbsp; &nbsp; &nbsp; "link": "https://api.trustpilot.com/v1/consumers/5899b3140000ff000a74b9e1"&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }]
打开App,查看更多内容
随时随地看视频慕课网APP