简体   繁体   中英

Unity Facebook SDK, retrieve data from 'apprequests' from API

I am fetching /me/apprequests from Facebook API. It works and I get the data in JSON format. Thing is that everything is nested, like a dictionary inside a dictionary.

What i tried:

    Dictionary<string, object> dict = Facebook.MiniJSON.Json.Deserialize(result.RawResult) as Dictionary<string, object>;
    object data;
    string request_code = "";
    if (dict.TryGetValue("data", out data))
    {
        var rc = (((Dictionary<string, object>)data)["id"]);
        request_code = (string)rc;
    }

    Debug.Log("request_code=" + request_code);

I think I need to loop the dictionary to get all id's.

I can confirm that if (dict.TryGetValue("data", out data)) does work correctly and get the dictionary of data arrays, but fails here (((Dictionary<string, object>)data)["id"]); with casting error.

Json looks like:

{
    "data": [{
        "application": {
            "category": "Games",
            "link": "https:\/\/www.facebook.com\/games\/?app_id=2523532533",
            "name": "game name",
            "id": "23432423423"
        },
        "created_time": "2019-02-27T16:01:15+0000",
        "from": {
            "name": "David boom",
            "id": "387923432423089962"
        },
        "message": "You are invited",
        "to": {
            "name": "Dusty Spice",
            "id": "10234324421033685"
        },
        "id": "413880842521239_10156578101000000"
    },
    {
        "application": {
            "category": "Games",
            "link": "https:\/\/www.facebook.com\/games\/?app_id=2523532533",
            "name": "game name",
            "id": "23432423423"
        },
        "created_time": "2019-02-27T14:12:41+0000",
        "from": {
            "name": "David boom2",
            "id": "387923432423089962"
        },
        "message": "You are invited",
        "to": {
            "name": "Dusty Spice",
            "id": "10234324421033685"
        },
        "id": "316676422209302_10156578101000000"
    }],
    "paging": {
        "cursors": {
            "before": "NDEzODgwODQyNTIxMjM5Ojc1OTQzODY4NAZDZD",
            "after": "MzE2Njc2NDIyMjA5MzAyOjc1OTQzODY4NAZDZD"
        }
    }
}

Managed to make it work if it will help someone:

 string request_code = "";

            if (dict.TryGetValue("data", out data))
            {
                int dataLength = ((List<object>)data).Count;
                for (int i = 0; i < dataLength; i++)
                {
                    var rc = ((List<object>)data)[i];
                    var rc2 = (((Dictionary<string, object>)rc)["id"]);
                    request_code = (string)rc2;
                    Debug.Log("request_code=" + request_code);
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM