This question already has an answer here:
- How can I parse JSON with C#? 12 answers
I am trying to pull a value from a rest api json response using C#.
I have the following code:
client.BaseUrl = "https://api.cloud.appcelerator.com";
request.Resource = "/v1/chats/create.json?key=" + cac.AppCode.ToString();
request.Method = Method.POST;
request.AddUrlSegment("appkey", "key");
var response = client.Execute(request);
In the "response" message I got a json content as follows:
{
"meta": {
"code": 200,
"status": "ok",
"method_name": "createChatMessage"
},
"response": {
"chats": [
{
"id": "521cfcd840926a0b3500449e",
"created_at": "2013-08-27T19:24:08+0000",
"updated_at": "2013-08-27T19:24:08+0000",
"message": " join to the chat group, welcome …",
"from": {
"id": "520f41e125e74b0b2400130a",
"first_name": "Administrator",
"created_at": "2013-08-17T09:26:57+0000",
"updated_at": "2013-08-27T19:23:10+0000",
"external_accounts": [
],
"email": "roy@tomax.co.il",
"confirmed_at": "2013-08-17T09:26:57+0000",
"username": "admin",
"admin": "true",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
}
},
"chat_group": {
"id": "521cfcd840926a0b3500449d",
"created_at": "2013-08-27T19:24:08+0000",
"updated_at": "2013-08-27T19:24:08+0000",
"message": " join to the chat group, welcome …",
"participate_users": [
{
"id": "520f41e125e74b0b2400130a",
"first_name": "Administrator",
"created_at": "2013-08-17T09:26:57+0000",
"updated_at": "2013-08-27T19:23:10+0000",
"external_accounts": [
],
"email": "roy@tomax.co.il",
"confirmed_at": "2013-08-17T09:26:57+0000",
"username": "admin",
"admin": "true",
"stats": {
"photos": {
"total_count": 0
},
"storage": {
"used": 0
}
}
}
]
}
}
]
}
}
How do I pull the following nested value of "id": "521cfcd840926a0b3500449e" from the returned json response result message?
I am using C#.
1> Add this namspace. using Newtonsoft.Json.Linq;
2> use this source code.
Create a C# class that maps to your Json and use
Newsoft JsonConvert
to Deserialise it.For example:
Step 1: a great tool - http://json2csharp.com/ - the results generated by it are below
Step 2:
JToken.Parse(...).ToObject<RootObject>()
.