Reputation: 248
I am attempting to parse a very long segment of JSON returned from a Helpdesk system, which I have no ability to modify. I have, thus far, been using the JavaScriptSerializer().Deserialize<Ticket>(data)
method in C#, but the problem I run into is the variance in JSON that will come back at me. Specifically, some properties have seemingly random sub-properties. What follows is a trimmed down version of obscured data illustrating this:
{"ticket_id":"12345","custom_fields":[{"customgroup_id":"12","custom_group":[{"customfield_id":"33","custom_field_data":{"customfield_data_id":"12345"}},{"customfield_id":"34","custom_field_data":{"customfield_data_id":"12345"}},{"customfield_id":"35","custom_field_data":{"3":{"customfield_data_id":"12345"}}},{"customfield_id":"54","custom_field_data":{"customfield_data_id":"12345"}},{"customfield_id":"55","custom_field_data":{"6":{"customfield_data_id":"12345"}}}]}]}
Properties not relevant have been removed. As one can see, sometimes custom_field_data
immediately follows with the array containing customfield_data_id
, but some times it is followed with a completely random integer, and then the array with customfield_data_id
. My problem is that I have no idea how to parse this -- thus far, I have simply mapped the JSON with corresponding classes, but considering the fact that it appears to be completely random whether or not those integers will get in between the custom_field_data
and its list, I haven't a clue how to parse this.
If there is a way that preserves my current work in making Class Maps, that would be preferred. If the only solution is to completely switch over to some other library/method, then so be it. Thank you all for the assistance!
Upvotes: 0
Views: 204
Reputation: 2000
If you can't create a C# class that match 100% with the JSON object. Your best option is to write your own custom JSON converter (System.Text.Json.Serialization.JsonConverter) as supported in .NET CORE. Have a look at this https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to#support-polymorphic-deserialization
Basically, it allows you to step through each JSON node 1 by 1 and you decide where to parse the value (put it in property of a class, or in Dictionary, or Array).
Upvotes: 1