2

I'm worinkg with instagram API and when I'm receiving recent media with any hashtag by this template: https://api.instagram.com/v1/tags/{hashtag}/media/recent I'm receiving data like this:

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


and so on. As You can see, object "data" is an Array, and further we can see object "tags", which is also array. how can I check number elements array of array in C#? i tried like this:

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

but it doesn't work. I parse JSON like this:

dynamic jsonData = JsonConvert.DeserializeObject<dynamic>(JSON_string);
2
  • 1
    Define "doesn't work" Commented Jul 27, 2015 at 17:49
  • I catch an exception: "Object reference not set to an instance of an object." Commented Jul 27, 2015 at 17:55

2 Answers 2

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

You can also use a JsonPath:

var token = JToken.Parse(str);
var count = token.SelectTokens("$.data[0].tags[*]").Count();
Sign up to request clarification or add additional context in comments.

1 Comment

@mati199537 No prob! I added a simpler way, using a JsonPath
0

Was working on a similar problem when I found this question. I happened to find a slightly different solution using System.Text.Json which I thought may be useful to any who stumbled across this question as I did:

var jsonObject = JsonNode.Parse(str) as JsonObject;
var tags = jsonObject["data"][0]["tags"] as JsonArray;
var tags = tags.Count;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.