Reputation: 609
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.
Upvotes: 0
Views: 455
Reputation: 1258
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"
}
}
}
]
Upvotes: 1
Reputation: 1744
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
Upvotes: 2