第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JsonConvert.DeserializeObject 數(shù)組問題

JsonConvert.DeserializeObject 數(shù)組問題

C#
手掌心 2022-11-21 20:05:35
我有這個(gè) 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; }}當(dāng)我嘗試時(shí):List<Event> events = JsonConvert.DeserializeObject<List<Event>>(jsonstring);我得到:無法將當(dāng)前 JSON 對象(例如 { "name": "value" } )反序列化為類型 'System.Collections.Generic.List`1[LambdaFeedFunctions.Trustpilot.Models.Event]' 因?yàn)樵擃愋托枰粋€(gè) JSON 數(shù)組(例如 [ 1,2,3]) 正確反序列化。要修復(fù)此錯(cuò)誤,請將 JSON 更改為 JSON 數(shù)組(例如 [1,2,3])或更改反序列化類型,使其成為普通的 .NET 類型(例如,不是像整數(shù)這樣的原始類型,也不是像這樣的集合類型一個(gè)數(shù)組或列表),可以從 JSON 對象反序列化。JsonObjectAttribute 也可以添加到類型以強(qiáng)制它從 JSON 對象反序列化。我不確定我需要更改什么才能使其正常工作。
查看完整描述

2 回答

?
FFIVE

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

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

您現(xiàn)在可以通過以下方式訪問您的活動(dòng):


rootEvents.events


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

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超7個(gè)贊

您的 JSON 有一個(gè)RootObject,這將需要另一個(gè)包裝器 RootObject類才能如前所述正確反序列化。


或者,如果您可以更改 JSON 結(jié)構(gòu)以發(fā)送數(shù)組(不帶RootObject),如下所示,它應(yīng)該可以正常解析。


[

  {

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

      }

    }

  }

]


查看完整回答
反對 回復(fù) 2022-11-21
  • 2 回答
  • 0 關(guān)注
  • 396 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號