gpt4 book ai didi

c# - 如何计算 C# 中数组中 JSON 元素的数量

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:28 26 4
gpt4 key购买 nike

我正在使用 instagram API,当我通过此模板接收带有任何主题标签的最新媒体时: https://api.instagram.com/v1/tags/ {标签}/媒体/最近我收到这样的数据:

{
"pagination": {
"next_max_tag_id": "any_number",
"deprecation_warning": "next_max_id and min_id are deprecated for this endpoint; use min_tag_id and max_tag_id instead",
"next_max_id": "any_number",
"next_min_id": "any_number",
"min_tag_id": "any_number",
"next_url": "https://api.instagram.com/v1/tags/{hashtag}/media/recent?access_token={my_personal_access-token}"
},
"meta": {
"code": 200
},
"data": [
{
"attribution": null,
"tags": [
"any_tag",
"any_tag1",
"any_tag2",
"any_tag3"
],
"type": "image",
"location": null,
"comments": {
"count": 0,
"data": []
},
"filter": "Normal",
"created_time": "any_number",
"link": "any_url",
"likes": {
"count": 0,
"data": []
},
"images": {
"low_resolution": {
"url": "any_url",
"width": 320,
"height": 320
},
"thumbnail": {
"url": "any_url",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "any_url",
"width": 640,
"height": 640
}
},
"users_in_photo": [],
"caption": {
"created_time": "any_number",
"text": "any_content",
"from": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
},
"id": "any_number"
},
"user_has_liked": false,
"id": "any_number",
"user": {
"username": "any_username",
"profile_picture": "any_url",
"id": "any_number",
"full_name": "any_full_name"
}
},


等等。如您所见,对象“data”是一个数组,进一步我们可以看到对象“tags”,它也是一个数组。如何在 C# 中检查数组的数字元素数组?我试过这样:

JArray items = (JArray)jsonData["data[0].tags"];
int length = items.Count;

但它不起作用。我这样解析 JSON:

dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(JSON_string);

最佳答案

var token = JToken.Parse(str);
var data = token.Value<JArray>("data");
var tags = data[0].Value<JArray>("tags");
var count = tags.Count;

您还可以使用 JsonPath :

var token = JToken.Parse(str);
var count = token.SelectTokens("$.data[0].tags[*]").Count();

关于c# - 如何计算 C# 中数组中 JSON 元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com