亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JsonConvert.DeserializeObject 數組問題

JsonConvert.DeserializeObject 數組問題

C#
手掌心 2022-11-21 20:05:35
我有這個 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 對象反序列化。我不確定我需要更改什么才能使其正常工作。
查看完整描述

2 回答

?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

使用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


查看完整回答
反對 回復 2022-11-21
?
三國紛爭

TA貢獻1804條經驗 獲得超7個贊

您的 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"

      }

    }

  }

]


查看完整回答
反對 回復 2022-11-21
  • 2 回答
  • 0 關注
  • 407 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號