Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

JSON nested access

Discussion in 'Scripting' started by Keysaw, Oct 22, 2015.

  1. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Hi everyone!

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

    Code (CSharp):
    1. {
    2.   "name": "Jake",
    3.   "feed": {
    4.     "data": [
    5.       {
    6.         "message": "Hello! ! ♡",
    7.         "story": "Jake with Mike and 2 others",
    8.         "id": "872470432869642_843613872446638",
    9.         "likes": {
    10.           "data": [
    11.             {
    12.               "name": "Jake",
    13.               "id": "10207764200433902"
    14.             },
    15.             {
    16.               "name": "Isabelle",
    17.               "id": "1686428204976478"
    18.             },
    19.             {
    20.               "name": "Lilou",
    21.               "id": "1656097109833376"
    22.             }
    23.           ],
    24.           "paging": {
    25.             "cursors": {
    26.               "after": "MTAyMLc3NDUwMTIxMjY5NDE=",
    27.               "before": "MTAyMDc3NjQyMRT0NDg4MDI="
    28.             }
    29.           }
    30.         }
    31.       },
    32.       {
    33.         "message": "10 days left",
    34.         "story": "Jake added 18 new photos to the album: Danemark.",
    35.         "id": "10207764200448802_10207209973799955",
    36.         "likes": {
    37.           "data": [
    38.             {
    39.               "name": "Josh",
    40.               "id": "10153051797757583"
    41.             },
    42.             {
    43.               "name": "Margot",
    44.               "id": "10207939314652242"
    45.             },
    46.             {
    47.               "name": "Iph",
    48.               "id": "997937874371968"
    49.             },
    50.             {
    51.               "name": "Prak",
    52.               "id": "994293789622585"
    53.             },
    54.             {
    55.               "name": "Din",
    56.               "id": "936043453109169"
    57.             },
    58.             {
    59.               "name": "John",
    60.               "id": "1040879992694764"
    61.             }
    62.           ],
    63.           "paging": {
    64.             "cursors": {
    65.               "after": "MTAyMDc1KOkzODYzNzM0MzU=",
    66.               "before": "MTAxNPLwNTE3OTg3NzE1ODM="
    67.             }
    68.           }
    69.         }
    70.       },
    71.       {
    72.         "message": "One year in Danemark...",
    73.         "story": "Jake added 13 new photos to the album: Danemark - 2014/2015.",
    74.         "id": "10207764200489002_10207889154588063",
    75.         "likes": {
    76.           "data": [
    77.             {
    78.               "name": "Marc",
    79.               "id": "10206559113890318"
    80.             },
    81.             {
    82.               "name": "Carole",
    83.               "id": "10153801245882446"
    84.             }
    85.           ],
    86.           "paging": {
    87.             "cursors": {
    88.               "after": "NzgyNjUxJUH1MTc4NDMy",
    89.               "before": "MTAyMDY1KLkxMTMzNTQzMTg="
    90.             },
    91.             "next": "https://graph.facebook.com/v2.4/1407536054_10207112154588063/likes?fields=name&access_token=CAAHTUph3fVkBAKHKoabXah0bdD3BT6Ai0ZCmoxdFeoQyOH7T5HKlJ3uHcPGUxNChCVz6Uo7LlIDtOLuy7LmsmnoZAaNZB5WLxH0m6KtHinpUTZAp5JzwZBERNZASZAZCu21fVyeA6oO426RmQ6NZCczz8jKYTLJxVeaJZC3tKVI6MecON6LKwMIeBpNPDBhX6rvtioQN4x77oKngZDZD&limit=25&after=NzgyNjUxMDU1MTc4NDMy"
    92.           }
    93.         }
    94.       }
    95.     ]
    96.   }
    97. }
    (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).
     
  2. S_Rank_Crazy

    S_Rank_Crazy

    Joined:
    Feb 3, 2014
    Posts:
    26
    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:

    Code (CSharp):
    1. var likes = data["likes"] as Dictionary<string, object>;
    2. // access likes data from here
    3. var likesData = likes["data"];
     
  3. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    Thank you for your help, but I didn't succeed... What is "data" in your "data["likes"]" ?

    Here how I deserialize the JSON :
    Code (CSharp):
    1. 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.
     
  4. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    I've been using JSON Object for a while now and it is great, not to mention free as well.

    https://www.assetstore.unity3d.com/en/#!/content/710

    Once you download this package and install it, you can handle JSON easily:
    Code (CSharp):
    1. string result = ...   //your facebook json response
    2.  
    3. JSONObject json = new JSONObject( result );
    4.  
    5. 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!
     
  5. Keysaw

    Keysaw

    Joined:
    Oct 14, 2015
    Posts:
    19
    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. :)

    EDIT : If I want to get the number of likes for each post, instead of getting the names, how should I do?
     
    Last edited: Oct 24, 2015
  6. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    You could count the number of people inside of the data