Select Token C# Json array
12,559
Solution 1
First you have to create model class You can use http://json2csharp.com/, and deserialize it using Json.NET
use the following namespace
using Newtonsoft.Json;
model class and DeserializeObject method
public class RootObject
{
public string character { get; set; }
public string credit_id { get; set; }
public int id { get; set; }
public string name { get; set; }
public string profile_path { get; set; }
public int order { get; set; }
}
var x =JsonConvert.DeserializeObject<List<RootObject>>(myjsondata);
var character = x[index].character;
Solution 2
Your JSON seems to be incorrect, you have to remove begin and end curly braces.
Correct JSON:
[{
"character": "Eddard Stark",
"credit_id": "5256c8ad19c2956ff60478a6",
"id": 48,
"name": "Sean Bean",
"profile_path": "/iIxP2IzvcLgr5WaTBD4UfSqaV3q.jpg",
"order": 0
}, {
"character": "Jon Snow",
"credit_id": "5256c8af19c2956ff6047af6",
"id": 239019,
"name": "Kit Harington",
"profile_path": "/dwRmvQUkddCx6Xi7vDrdnQL4SJ0.jpg",
"order": 0
}, {
"character": "Robert Baratheon",
"credit_id": "5256c8ad19c2956ff60478e2",
"id": 13633,
"name": "Mark Addy",
"profile_path": "/tGWYaLPIGvPJiKx9KzTBMITo7uK.jpg",
"order": 1
}, {
"character": "Daenerys Targaryen",
"credit_id": "5256c8af19c2956ff60479f6",
"id": 1223786,
"name": "Emilia Clarke",
"profile_path": "/tB1nE2LJH81f5UMiGhKCSlaqsF1.jpg",
"order": 1
}, {
"character": "Tyrion Lannister",
"credit_id": "5256c8b219c2956ff6047cd8",
"id": 22970,
"name": "Peter Dinklage",
"profile_path": "/xuB7b4GbARu4HN6gq5zMqjGbkwF.jpg",
"order": 2
}]
Equivalent Model class:
public class SampleClass
{
public string character { get; set; }
public string credit_id { get; set; }
public int id { get; set; }
public string name { get; set; }
public string profile_path { get; set; }
public int order { get; set; }
}
Deserialize using the below code:
It will give you List
or Array
of SampleClass
object. You can use foreach
loop or LINQ
to get all the character
field.
List<SampleClass> lsObj = JsonConvert.DeserializeObject<List<SampleClass>>(strJson);
foreach(SampleClass obj in lsObj)
{
string character = obj.character;
}
Author by
Mayamiko
Updated on June 04, 2022Comments
-
Mayamiko almost 2 years
I have the code below and i want to select the child note when i am accessing 'a'. But string result is returning null when i select the 'character' node
JObject jObj = (JObject)JsonConvert.DeserializeObject(strJson); Assert.IsTrue(response.IsSuccessStatusCode.Equals(true)); var a = (JArray)jObj["cast"]; string result = (string)a.SelectToken("character");
This is the JSON response i get from 'a':
{[ { "character": "Eddard Stark", "credit_id": "5256c8ad19c2956ff60478a6", "id": 48, "name": "Sean Bean", "profile_path": "/iIxP2IzvcLgr5WaTBD4UfSqaV3q.jpg", "order": 0 }, { "character": "Jon Snow", "credit_id": "5256c8af19c2956ff6047af6", "id": 239019, "name": "Kit Harington", "profile_path": "/dwRmvQUkddCx6Xi7vDrdnQL4SJ0.jpg", "order": 0 }, { "character": "Robert Baratheon", "credit_id": "5256c8ad19c2956ff60478e2", "id": 13633, "name": "Mark Addy", "profile_path": "/tGWYaLPIGvPJiKx9KzTBMITo7uK.jpg", "order": 1 }, { "character": "Daenerys Targaryen", "credit_id": "5256c8af19c2956ff60479f6", "id": 1223786, "name": "Emilia Clarke", "profile_path": "/tB1nE2LJH81f5UMiGhKCSlaqsF1.jpg", "order": 1 }, { "character": "Tyrion Lannister", "credit_id": "5256c8b219c2956ff6047cd8", "id": 22970, "name": "Peter Dinklage", "profile_path": "/xuB7b4GbARu4HN6gq5zMqjGbkwF.jpg", "order": 2 } ]}