neildeadman
neildeadman

Reputation: 3969

Trouble getting at values in JSON with json.net in c#

I am trying to use an RESTFUL API for an application we use internally. One call to the API returns the following JSON:

{
    "operation": {
        "name": "GET RESOURCES",
        "result": {
            "status": "Success",
            "message": "Query was successful"
        },
        "totalRows": 2,
        "Details": [{
            "RESOURCE DESCRIPTION": "Windows",
            "RESOURCE TYPE": "Windows",
            "RESOURCE ID": "101",
            "RESOURCE NAME": "WINDOWSPC",
            "NOOFACCOUNTS": "1"
        }, {
            "RESOURCE DESCRIPTION": "Ubuntu",
            "RESOURCE TYPE": "Linux",
            "RESOURCE ID": "808",
            "RESOURCE NAME": "UBUNTUPC",
            "NOOFACCOUNTS": "2"
        }]
    }
}

Using json.net I deseralize the json and check the stats with the following lines:

dynamic json = JsonConvert.DeserializeObject(response);
var status = json.operation.result.status.Value;

Next I want to get each value of each of the "Details" returned, but I cannot figure out how. I first tried getting the Details only with this:

var resourceList = json.operation.Details

Which works, but I cannot iterate over this to get just the "RESOURCE ID" and "RESOURCE NAME" for example.

I cannot use .Children() either, but when I hover over the resourceList there is a ChildrenTokens which seems to be what I want, but I cannot get at that in my code.

I also tried using resourceList as a DataSet as per their example but it throws an exception.

Can someone see what I am doing wrong..... I am not familiar with parsing JSON in C#

Upvotes: 1

Views: 472

Answers (3)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23298

You can use Json.Linq for that and parse a response into JObject, then iterate it foreach loop. It's possible, since Details is an array and JObject implements IDictionary<string, JToken> and IEnumerable<KeyValuePair<string, JToken>>

var jObject = JObject.Parse(response);

foreach (var detail in jObject["operation"]["Details"])
{
    var description = detail["RESOURCE DESCRIPTION"].Value<string>();
    //other properties
}

Upvotes: 2

Rahul Sharma
Rahul Sharma

Reputation: 8311

It is as simple as this:

Your Model classes would look like:

public class Result
{
    public string status { get; set; }
    public string message { get; set; }
}

public class Detail
{
    [JsonProperty("RESOURCE DESCRIPTION")]
    public string ResourceDescription { get; set; }
    [JsonProperty("RESOURCE TYPE")]
    public string ResourceType { get; set; }
    [JsonProperty("RESOURCE ID")]
    public string ResourceId { get; set; }
    [JsonProperty("RESOURCE NAME")]
    public string ResourceName { get; set; }
    [JsonProperty("NOOFACCOUNTS")]
    public string NoOfAccounts { get; set; }
}

public class Operation
{
    public string name { get; set; }
    public Result result { get; set; }
    public int totalRows { get; set; }
    public List<Detail> Details { get; set; }
}

public class RootObject
{
    public Operation operation { get; set; }
}

To de-serialize:

var json = JsonConvert.DeserializeObject<RootObject>(response);

To access a property:

var name=json.operation.name

To access your Details:

foreach(var item in json.operation.Details)
{
var myresourcename=item.ResourceName;
//So on
}

Upvotes: 1

Innat3
Innat3

Reputation: 3576

Here's an example using the JObject class instead of dynamic

JObject json = JObject.Parse(response);

string status = json["operation"]["result"]["status"].Value<string>();

foreach (JToken resource in json["operation"]["Details"])
{
    string id = resource["RESOURCE ID"].Value<string>();
    string name = resource["RESOURCE NAME"].Value<string>();
}

Upvotes: 1

Related Questions