hakani
hakani

Reputation: 11

JSON select child tag c#

How can I get the id value of the child values in the following code?

I am using c#.

I get the value of the result, but I can not get the value was the result "id".

Thanks.

var jobject = JObject.Parse(responseString);
var dataObj = (JObject)jobject["data"];
var rsltArr = (JArray)dataObj["result"];

My Json:

{  
    "data":{  
        "result":[  
            {  
                "id":"aa:text:20150226:4642933",
                "type":"text",
                "date":"2015-02-26T06:33:36Z",
                "title":"Karayollar\u0131nda durum"
            },
            {  
                "id":"aa:text:20150226:4642933",
                "type":"text",
                "date":"2015-02-26T06:33:36Z",
                "title":"Karayollar\u0131nda durum"
            }
        ]
    }
}

Upvotes: 0

Views: 1463

Answers (1)

stefankmitph
stefankmitph

Reputation: 3306

This gets you the id:

dynamic o = JsonConvert.DeserializeObject(yourJsonString);
Console.WriteLine(o.data.result[0].id);

you could also deserialize your Json to a class:

    public class Result
    {
        public string id { get; set; }
        public string type { get; set; }
        public DateTime date { get; set; }
        public string title { get; set; }
    }

    public class Data
    {
        public IList<Result> result { get; set; }
    }

    public class MyJsonObject
    {
        public Data data { get; set; }
    }

And then access it this way...

MyJsonObject myJsonObject = JsonConvert.DeserializeObject<MyJsonObject>(json);

// do some null checking here
foreach (var item in myJsonObject.data.result)
{
    Console.WriteLine(item.id);                
}

Upvotes: 2

Related Questions