I have a COMPLEX JSON which I need to deserialize and access all its objects. My JSON looks like this
{
"name": {},
"percentage": [
{
"math": {
"2.503300099.1": 460800,
"2.503300099.2": 460800,
"2.503300099.3": 460800,
"2.503308292.1": 2457600,
"2.503308292.2": 2457600,
"2.503308292.3": 2457600
},
"english": {
"2.503300099": "hello",
"2.503308292": "hi"
},
"exam": "2.0"
}
],
"class": {},
"section": "7.17.1",
"total": 1
}
The values of JSON may change, number of times they appear may also change.
What I can access so far is name,class,section,total,and issue is with percentage part. I can reach till percentage also but I cant access math or English, or exam with their values.
string content = await response.Content.ReadAsStringAsync();
JObject Search = JObject.Parse(content);
Speed res = new Speed();
res.class= Search["class"].ToString(); // good
res.english = Search["english"].ToString();// not good
Any help or suggestion please ?
EDIT
After so many questions of the structure of JSON, I would like to clarify that structure wont changes ever but the number of objects may, lets understand this with an example:
{
"One": {},
"Two": [
{
"twoA": {
"2.503300099.1": 460800,
"2.503300099.2": 460800,
"...." : ...,
"...." : ...,
},
"TwoB": {
"2.503300099": "value",
"2.503308292": "value"
"...." : ...,
"...." : ...,
},
"TwoC": {
"2.503300099": "value",
"2.503308292": "value"
"...." : ...,
"...." : ...,
},
"exam": "2.0",
"...." : ...,
"...." : ...,
}
],
"Three": {empty},
"Four": "value string",
"Five": value int
.....
.....
}
Hope this clarifies doubt on JSON structure
Judging from the comments on your question and subsequent answers it would seem that the data from the API is unpredictable to the extent that I'd begin arguing that the people responsible for that API should take a step back and rethink their approach. I don't see how you could ever parse any data without as much as hint as to how that data might be structured.
But maybe I'm overreacting...
Maybe parsing the data as a
dynamic
is the answer you're looking for? Using this approach, you can do things like:The path for a value in
math
in your example, if I'm not mistaken, would be something like:Not very readable though. Or stable. So, I reside, back to ponder my original quandary of what manner of creature thought up the API in question...
EDIT: Updates to reflect comments and changes in the original question:
Given the structure you present in your second example, you could also define suitable types to fit that same structure:
Then you can deserialize and parse like this:
Because the property names in the source data are not valid for C#, you still need the
dynamic
typing in those places where problems would otherwise occur (e.g.MyNextObject.twoA
). Also, depending on what you're trying to do and in what context, enumerating the children ofpercentage
etc. may be a better choice than going for specific indexes.Note that I'm using the exact same json structure as you presented in all my examples, albeit shortened in the end for brevity and escaped to present working examples.
Where your source property names are inconsistent, invalid or not adhering common praxis, to keep your C# code clean, you should probably use some method of mapping values between source and target. E.g. if you're using Json.NET, things like the
JsonProperty
attribute.Also, please not that I'm not presenting "production like code" here. You should obviously take care and handle potential problems such as
null
values or out-of-bound indexes properly.