簡體   English   中英

Unity Facebook SDK,從 API 的“apprequests”中檢索數據

[英]Unity Facebook SDK, retrieve data from 'apprequests' from API

我正在從 Facebook API 獲取 /me/apprequests。 它有效,我以 JSON 格式獲取數據。 事情是一切都是嵌套的,就像字典中的字典一樣。

我試過的:

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

我想我需要循環字典來獲取所有的 id。

我可以確認if (dict.TryGetValue("data", out data))是否正常工作並獲取數據數組的字典,但在這里失敗(((Dictionary<string, object>)data)["id"]); 與鑄造錯誤。

Json 看起來像:

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

設法使其工作,如果它會幫助某人:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM