选择标记C# Json数组

t30tvxxf  于 2023-01-22  发布在  C#
关注(0)|答案(3)|浏览(143)

我有下面的代码,我想在访问'a'时选择子节点,但是当我选择'character'节点时,字符串结果返回null

JObject jObj = (JObject)JsonConvert.DeserializeObject(strJson);
Assert.IsTrue(response.IsSuccessStatusCode.Equals(true));
var a = (JArray)jObj["cast"];
string result = (string)a.SelectToken("character");

这是我从'a'得到的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
}
]}
yhxst69z

yhxst69z1#

您的JSON似乎是不正确的,您必须删除开始和结束花括号。
正确的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
}]

等效型号分类:

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

使用以下代码进行反序列化:
它会给予你ListSampleClass对象的Array。你可以使用foreach循环或LINQ来获取所有的character字段。

List<SampleClass> lsObj = JsonConvert.DeserializeObject<List<SampleClass>>(strJson);

    foreach(SampleClass obj in lsObj)
    {
        string character = obj.character;
    }
xxe27gdn

xxe27gdn2#

首先,您必须创建模型类。您可以使用http://json2csharp.com/,并使用Json.NET对其进行反序列化
使用以下名称空间

using Newtonsoft.Json;

模型类和DeserializeObject方法

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

hfsqlsce3#

你可以直接用JObject.Parse()方法或者JArray.Parse()把字符串解析成JObject,然后你可以遍历数组,通过索引得到数组对象:如果你想得到这个对象的某个特定值--使用Value<T>()方法^jsonObj[0].Value<string>("character")

相关问题