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

时间:2015-07-27 17:48:27

标签: c# arrays json json-deserialization

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>

int main()
{
    DIR* dir;
    FILE *entry_file;
    struct dirent *in_file;
    char files[1000];
    int i;

    dir = opendir("/Users/tcn/data");

    if(dir==NULL){
        printf("Error! Unable to read directory");
        exit(1);
    }

    while( (in_file=readdir(dir)) != NULL) {
        if (!strcmp (in_file->d_name, "."))
            continue;
        if (!strcmp (in_file->d_name, ".."))
            continue;
        printf("%s\n", in_file->d_name);
        entry_file = fopen(in_file->d_name, "r");

        fscanf(entry_file, "%s", files); 
    }

    closedir(dir);
    fclose(entry_file);
    return 0;
}


等等。 如您所见,对象&#34;数据&#34;是一个数组,我们还可以看到对象&#34;标签&#34;,它也是数组。如何在C#中检查数组的数组元素数组?我试过这样:

{
  "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"
      }
    },

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

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

1 个答案:

答案 0 :(得分:3)

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