0

I have this JSON which i'm trying to deserialize:

{
  "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"
        }
      }
    }
  ]
}

These are my models:

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

When I try:

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

I get:

Cannot deserialize the current JSON object (e.g. { "name": "value" } ) into type 'System.Collections.Generic.List`1[LambdaFeedFunctions.Trustpilot.Models.Event]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

I'm not sure what I need to change to make it work.

croxy
  • 4,082
  • 9
  • 28
  • 46
Ben
  • 609
  • 6
  • 21
  • Possible duplicate of [How to show the "paste Json class" in visual studio 2012 when clicking on Paste Special?](https://stackoverflow.com/questions/18526659/how-to-show-the-paste-json-class-in-visual-studio-2012-when-clicking-on-paste) – mjwills Apr 01 '19 at 09:09
  • 7
    You are missing a `RootObject`. Paste JSON as Classes will solve that for you. – mjwills Apr 01 '19 at 09:09
  • 1
    An alternative to Visual Studios *paste special* function would be [json2csharp](http://json2csharp.com/). This will generate an appropriate class model to a given json string. – croxy Apr 01 '19 at 09:14

2 Answers2

2

Use 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; }
}

Then

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

You can now access your events as:

rootEvents.events
ALFA
  • 1,726
  • 1
  • 10
  • 19
1

Your JSON has a RootObject, which would require another a wrapper RootObject class to deserialize correctly as mentioned.

Alternatively if you can change the JSON structure to send an array (without the RootObject) like below, it should resolve fine.

[
  {
    "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"
      }
    }
  }
]
JvR
  • 1,022
  • 1
  • 11
  • 29