0

I am trying to serialize a JSON result from an API. The result has the following structure:

{
 "Version" : "123",
 "Message" : "some string",
 "Status" : 200,
 "Result" : { 
       "Events" :  [ {"Subprop1" : "some_value"}, {"Subprop2" : "some_value"} ],
       "Merch" :   [ {"Subprop2" : "some_value"}, {"Subprop2" : "some_value"} ],
       "Tickets" : [ {"Subprop3" : "some_value"}, {"Subprop2" : "some_value"} ],
       "Seasons" : [ {"Subprop4" : "some_value"}, {"Subprop2" : "some_value"} ] 
 }
}

My question is: how do I deserialize this properly into objects of the classes I made ? I've made classes for each of Result's proprieties (i.e. Event, Merch etc)

My code so far:

dynamic resultJSON = JsonConvert.DeserializeObject<dynamic>(response.Content);
dynamic data = resultJSON.Result;
Newtonsoft.Json.Linq.JArray events = resultJSON.Result.Events;

resultJSON contains everything in an object. How do I deserialize this in their own classes? And avoid JArray

Update here is my Event class

    public class Event
    {
      public int OrderId { get; set; }
      public int Id { get; set; }
      public string Name{ get; set; }
      public string Type { get; set; }
    
}

public class ListEvents
{
    public List<Event> Events { get; set; }
}
7
  • 4
    Have you tried making a Class to represent your data structure, rather than dynamic?
    – gunr2171
    Commented Dec 9, 2020 at 16:14
  • Yes, but for Events/Merch etc. Should I do it for the whole response ?
    – crystyxn
    Commented Dec 9, 2020 at 16:15
  • You can make Result a dictionary. stackoverflow.com/questions/1207731/…
    – gunr2171
    Commented Dec 9, 2020 at 16:15
  • I am getting "'Cannot implicitly convert type 'Newtonsoft.Json.Linq.JObject' to 'System.Collections.Generic.Dictionary<string,object>''" when trying Dictionary<string, dynamic> data = resultJSON.Result;
    – crystyxn
    Commented Dec 9, 2020 at 16:19
  • 2
    Can you post your classes? Also, stop using dynamic.
    – gunr2171
    Commented Dec 9, 2020 at 16:20

2 Answers 2

0

Define Json as Classes:

public class Response
{
    public string Version { get; set; }
    public string Message { get; set; }
    public int Status { get; set; }
    public Result Result { get; set; }
}

public class Result
{
    public Event[] Events { get; set; }
    public Merch[] Merch { get; set; }
    public Ticket[] Tickets { get; set; }
    public Season[] Seasons { get; set; }
}

public class Event
{
    public string Subprop1 { get; set; }
    public string Subprop2 { get; set; }
}

public class Merch
{
    public string Subprop2 { get; set; }
}

public class Ticket
{
    public string Subprop3 { get; set; }
    public string Subprop2 { get; set; }
}

public class Season
{
    public string Subprop4 { get; set; }
    public string Subprop2 { get; set; }
}

If your using Visual Studio you can copy the json and use Edit > Paste Special > Paste JSON as Classes

Then deserialize :

var response = JsonSerializer.Deserialize<Response>(jsonString);
1
  • this worked! Ive spent some time to make sure all properties are mapped correctly and now the deserialization works as it should , thank you
    – crystyxn
    Commented Dec 9, 2020 at 16:52
0

Assuming the values in the items in the Events array in the JSON actually match the fields in the Event class you've shown (rather than the dummy data in your example), then you can use JArray's ToObject method to convert the JArray into a list of events.

This avoids having to generate C# classes to represent other parts of the data which you aren't really interested in:

    dynamic resultJSON = JsonConvert.DeserializeObject<dynamic>(json);
    ListEvents list = new ListEvents();
    list.Events = ((JArray)resultJSON.Result.Events).ToObject<List<Event>>();

Demo: https://dotnetfiddle.net/nr5ME3

Not the answer you're looking for? Browse other questions tagged or ask your own question.