I'm working on an ASP.NET MVC app. My app is interacting with a third-party REST service. That service is getting called in the controller of my MVC app. The results from the service look like this:
{ "[email protected]": "#Collection", "group": [], "[email protected]": "#Collection", "class":[ { "total": 111, "value": "A" }, { "total": 222, "value": "B" }, { "total": 333, "value": "C" } ], "[email protected]": "#Collection", "status": [ { "total": 1, "value": "Open" }, { "total": 20, "value": "Closed" }, { "total": 51, "value": "Unknown" } ] }
The results of the service are stored in a JObject
property in my model called Results
. For every array, I am trying to print out its key name. Then, I want to look through and print out each value and total in the array. In other words, the JSON above would look like this:
group class A - 111 B - 222 C - 333 status Open - 1 Closed - 20 Unknown - 51
In an attempt to do this, I have the following in my View.
foreach (var result in Model.Results) { <div></div> <ul> @foreach (var subResult in result.?) { <li style="padding-left:8px;">@(subResult["value"] + " - " + subResult["total"])</li> } </ul> }
Clearly the above doesn't work. My challenge is, I do not understand how to:
- Loop through the key/value pairs in a JObject.
- Identify if the value is a JArray or another JObject.
If I use result.Children()
, I do not get each key/value pair like I'm expecting. At the same time, result
does not have a Keys
property like I would expect. I feel very stuck at the moment.
Thank you for any help you can provide. Happy holidays!