Mayamiko Mayamiko - 7 months ago 90
C# Question

Select Token C# Json array

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);
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

mww mww

First you have to create model class You can use, 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;