In C #, how can I model a JSON object with multiple nested arrays?

I get this JSON response from the system I'm connecting to, and try to find a better way to deserialize it into a C # object. I am currently using RestSharp , which seems pretty straight forward, but the JSON format puzzles me a bit. Here is the format it comes in:

[ {"name": "Tickets:", "schema": [ {"dataType": "string", "colName": "First", "idx": 0}, {"dataType": "string", "colName": "Second", "idx": 1}, {"dataType": "string", "colName": "Name", "idx": 2} ], "data": [ ["bill", "test", "joe"], ["bill2", "test2", "joe2"], ["bill3", "test3", "joe3"] ] } ] 

Here is my current code:

 var url = "http://myUrl:10111"; var client = new RestClient { BaseUrl = url }; var request = new RestRequest { Method = Method.GET, Resource = "/search?fmt=Json", RequestFormat = DataFormat.Json }; request.AddHeader("accept", "application/json"); var response = client.Execute(request); var wptResponse = new JsonDeserializer().Deserialize<TicketResults>(response); return wptResponse; 

but as stated above, I am trying to figure out the correct way to model the TicketResults object to support deserializing this message above.

Ideally, I would like something like this:

  public class TicketResults { public List<Ticket> Tickets {get;set;} } public class Ticket { public string First {get;set;} public string Second {get;set;} public string Name {get;set;} } 

and in this example above you will get three entries in the ticket collection.

Also, the above JSON format is normal, as I have never seen it break into a separate section of the schema and data (I can see where it can save some space, but in this case the messages are not so big)

+6
source share
3 answers

I agree that the json format is pretty ... dumb. Here's how to model your dto:

  public class JsonDto { public string name { get; set; } public Schema[] schema {get; set;} public string[][] data { get; set; } } public class Schema { public string dataType { get; set; } public string colName { get; set; } public int idx { get; set; } } 

I was able to get your string (no change) for JSON.Net deserialization as follows:

 var jsonDto = JsonConvert.DeserializeObject<JsonDto[]>(json); 

Let me know if you still have problems.

+4
source

In Visual Studio 2012 and above, and you can go to Edit > Paste Special > Paste JSON as classes . It produces the following code in your example pasted from the clipboard.

 public class Rootobject { public Class1[] Property1 { get; set; } } public class Class1 { public string name { get; set; } public Schema[] schema { get; set; } public string[][] data { get; set; } } public class Schema { public string dataType { get; set; } public string colName { get; set; } public int idx { get; set; } } 

 string json = File.ReadAllText("json.txt"); Rootobject root = new Rootobject(); root.Property1 = JsonConvert.DeserializeObject<Class1[]>(json); 
+14
source

Do you have control over the structure of the returned JSON? It's kind of stupid. For some reason, field names and data are separated. If the format was a little more reasonable, for example:

 [ { "First": "bill", "Second": "test", "Name": "joe" }, { "First": "bill2", "Second": "test2", "Name": "joe2" }, ] 

You can then serialize in your Ticket class. However, without reworking the JSON structure, which I do not recommend to you, the C # class that you serialize must conform to the JSON structure.

I suppose you could come up with a mediation class for storing JSON data as it comes to you. Then you can loop over these objects and create Ticket instances from them. At least in this way you get a data structure that you can work with.

+1
source

Source: https://habr.com/ru/post/973672/


All Articles