JsonConvert.DeserializeObject数组问题

时间:2019-04-01 09:06:33

标签: c# json.net

我有这个要反序列化的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 个答案:

答案 0 :(得分:2)

使用Json2CSharp

$collection1 = $collection1->groupBy('typessesion_id');

$result = $collection2->map(function($item, $key) {
    $typesession_id = $item->typesession_id;
    $item_clone = $item;
    $data = $collection1[$typesession_id]->map(function($it, $k) use ($item_clone) {
        if ($typesession_id == $it->typesession_id) {
            return $item_clone->merge($it);
        }

        return $it;
    });

    return $data->merge($item);
});

dd($result);

然后

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

答案 1 :(得分:1)

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