解析JSON响应时出错

时间:2022-05-14 21:30:28

I have this code below that gets a JSON response from an API call.

我在下面的代码中获取了API调用的JSON响应。

I have serialized it and I am trying to use Linq to Get a specific parent node and search through that node to find specific text.

我已将其序列化,我正在尝试使用Linq获取特定的父节点并搜索该节点以查找特定文本。

HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response =   client.GetAsync(some url).Result;
string strJson = response.Content.ReadAsStringAsync().Result;
object jObj = (JObject)JsonConvert.DeserializeObject(strJson);
Assert.IsTrue(response.IsSuccessStatusCode.Equals(true));
var a = JArray.Parse(jObj.ToString());
var Cast = a.SelectMany(x => x["episodes"]);

I Get an error:

我收到一个错误:

Object is not Json Array

对象不是Json数组

Any ideas of how I could do this?

有关如何做到这一点的任何想法?

Json response

Json回应

{
"cast": [
    {
      "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
    }
  ],
  "crew": [
    {
      "id": 93223,
      "credit_id": "5256c8a219c2956ff6046f0b",
      "name": "Brian Kirk",
      "department": "Directing",
      "job": "Director",
      "profile_path": null
    },
    {
      "id": 59984,
      "credit_id": "54eef41d9251417971005b8d",
      "name": "Marco Pontecorvo",
      "department": "Camera",
      "job": "Director of Photography",
      "profile_path": null
    },
    {
      "id": 1204180,
      "credit_id": "54eef453c3a3680b80006153",
      "name": "Frances Parker",
      "department": "Editing",
      "job": "Editor",
      "profile_path": null
    },
    {
      "id": 9813,
      "credit_id": "5256c8a019c2956ff6046e2b",
      "name": "David Benioff",
      "department": "Writing",
      "job": "Writer",
      "profile_path": "/8CuuNIKMzMUL1NKOPv9AqEwM7og.jpg"
    },
    {
      "id": 228068,
      "credit_id": "5256c8a219c2956ff6046e4b",
      "name": "D. B. Weiss",
      "department": "Writing",
      "job": "Writer",
      "profile_path": "/caUAtilEe06OwOjoQY3B7BgpARi.jpg"
    }
  ]
}

1 个解决方案

#1


0  

Try this:

尝试这个:

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

instead of :

代替 :

object jObj = (JObject)JsonConvert.DeserializeObject(strJson);
Assert.IsTrue(response.IsSuccessStatusCode.Equals(true));
var a = JArray.Parse(jObj.ToString());

It's already deserialized so no need to parse it anymore.

它已经反序列化,因此不再需要解析它。

Acces the a array further, example:

进一步访问数组,例如:

foreach (var actor in a)
{
    Console.WriteLine(actor["character"]);
}

Alternatively you can use the dynamic functionality:

或者,您可以使用动态功能:

dynamic jObj = JsonConvert.DeserializeObject(strJson);
var a = jObj.cast;

#1


0  

Try this:

尝试这个:

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

instead of :

代替 :

object jObj = (JObject)JsonConvert.DeserializeObject(strJson);
Assert.IsTrue(response.IsSuccessStatusCode.Equals(true));
var a = JArray.Parse(jObj.ToString());

It's already deserialized so no need to parse it anymore.

它已经反序列化,因此不再需要解析它。

Acces the a array further, example:

进一步访问数组,例如:

foreach (var actor in a)
{
    Console.WriteLine(actor["character"]);
}

Alternatively you can use the dynamic functionality:

或者,您可以使用动态功能:

dynamic jObj = JsonConvert.DeserializeObject(strJson);
var a = jObj.cast;