JSON nested access

Hi everyone!

I would like to read the data inside this JSON result that I have, using MiniJSON from Facebook.

{
  "name": "Jake",
  "feed": {
    "data": [
      {
        "message": "Hello! ! ♡",
        "story": "Jake with Mike and 2 others",
        "id": "872470432869642_843613872446638",
        "likes": {
          "data": [
            {
              "name": "Jake",
              "id": "10207764200433902"
            },
            {
              "name": "Isabelle",
              "id": "1686428204976478"
            },
            {
              "name": "Lilou",
              "id": "1656097109833376"
            }
          ],
          "paging": {
            "cursors": {
              "after": "MTAyMLc3NDUwMTIxMjY5NDE=",
              "before": "MTAyMDc3NjQyMRT0NDg4MDI="
            }
          }
        }
      },
      {
        "message": "10 days left",
        "story": "Jake added 18 new photos to the album: Danemark.",
        "id": "10207764200448802_10207209973799955",
        "likes": {
          "data": [
            {
              "name": "Josh",
              "id": "10153051797757583"
            },
            {
              "name": "Margot",
              "id": "10207939314652242"
            },
            {
              "name": "Iph",
              "id": "997937874371968"
            },
            {
              "name": "Prak",
              "id": "994293789622585"
            },
            {
              "name": "Din",
              "id": "936043453109169"
            },
            {
              "name": "John",
              "id": "1040879992694764"
            }
          ],
          "paging": {
            "cursors": {
              "after": "MTAyMDc1KOkzODYzNzM0MzU=",
              "before": "MTAxNPLwNTE3OTg3NzE1ODM="
            }
          }
        }
      },
      {
        "message": "One year in Danemark...",
        "story": "Jake added 13 new photos to the album: Danemark - 2014/2015.",
        "id": "10207764200489002_10207889154588063",
        "likes": {
          "data": [
            {
              "name": "Marc",
              "id": "10206559113890318"
            },
            {
              "name": "Carole",
              "id": "10153801245882446"
            }
          ],
          "paging": {
            "cursors": {
              "after": "NzgyNjUxJUH1MTc4NDMy",
              "before": "MTAyMDY1KLkxMTMzNTQzMTg="
            },
            "next": "https://graph.facebook.com/v2.4/1407536054_10207112154588063/likes?fields=name&access_token=CAAHTUph3fVkBAKHKoabXah0bdD3BT6Ai0ZCmoxdFeoQyOH7T5HKlJ3uHcPGUxNChCVz6Uo7LlIDtOLuy7LmsmnoZAaNZB5WLxH0m6KtHinpUTZAp5JzwZBERNZASZAZCu21fVyeA6oO426RmQ6NZCczz8jKYTLJxVeaJZC3tKVI6MecON6LKwMIeBpNPDBhX6rvtioQN4x77oKngZDZD&limit=25&after=NzgyNjUxMDU1MTc4NDMy"
          }
        }
      }
    ]
  }
}

(I got this JSON from Facebook Graph API. Basically, there is three posts, and the usernames of the persons who liked each of them)

My problem is : I can access to the data for each post (like “message” or “story”), because it’s in the array “data” of the key “feed”, BUT I cannot access the infos of the “likes” array, probably because it’s kind of an array inside an array…

Any idea of how I can access it?

Thank you (I can post more of my code if needed, I just didn’t want to overload this thread).

When you deserialise I assume you’re storing the result in a Dictionary<string, object>? All you need to do is cast data[“likes”] to the same:

var likes = data["likes"] as Dictionary<string, object>;
// access likes data from here
var likesData = likes["data"];

Thank you for your help, but I didn’t succeed… What is “data” in your “data[“likes”]” ?

Here how I deserialize the JSON :

var JSONresult = Json.Deserialize(result.Text) as Dictionary<string, object>;

So from there, I can get the “name” and “feed” keys, but not the ones on a lower level.

What I understand (and I hope I’m wrong) is that I should create a new dictionary for each level in my JSON file ?

Sorry for my lack of understanding, I don’t really get how the Dictionary function works so far.

I’ve been using JSON Object for a while now and it is great, not to mention free as well.

Once you download this package and install it, you can handle JSON easily:

string result = ...   //your facebook json response

JSONObject json = new JSONObject( result );

string likeName = json["feed"]["data"][0]["likes"]["data"][1]["name"].str;

Just note that JSON Object parses his content into JSONObject as well. Thats why at the end we have to use .str to get string value. Hope it helps!

I’ve just tried with JSONObject and it seems to be working ! Thank you very much, I was hoping to find this kind of concatenation writing.

I will go deeper with it to see if I can manage that properly. :slight_smile:

EDIT : If I want to get the number of likes for each post, instead of getting the names, how should I do?

You could count the number of people inside of the data