Unity Facebook AppRequestCallback

时间:2014-03-14 17:17:27

标签: c# facebook facebook-unity-sdk

在发送应用请求后,我遇到了获取用户ID的问题。 在facebook手册中,它被描述为
“已创建的请求的收件人用户标识的数组。”
https://developers.facebook.com/docs/unity/reference/current/FB.Apprequest
而我在回调中的代码就像:
if(responseObject.TryGetValue ("to", out to)) { string[] output = (string[])to; print(output.Length); } 其中responseObject是反序列化的FBResult 但是这给了我并且错误说:
“InvalidCastException:无法从源类型转换为目标类型。”
我也在对象中尝试过foreach字符串,但它只会转换。
我可以获得一些帮助,以获取我已发送请求的用户数量。
非常感谢你。

2 个答案:

答案 0 :(得分:0)

首先将其转换为对象列表

if(responseObject.TryGetValue ("to", out to)) { 


    List<object> output = (List<object>)to; 

    print(output.Count);

    foreach(object uid in output) {
        print(uid.ToString());
    }

}

答案 1 :(得分:0)

private void RequestFriendsCallback(IGraphResult result)
{
    if (!string.IsNullOrEmpty(result.RawResult))
    {
        var resultDictionary = result.ResultDictionary;
        if (resultDictionary.ContainsKey("data"))
        {
            var dataArray = (List<object>)resultDictionary["data"];//2.1.4
            var dic = dataArray.Select(x => x as Dictionary<string, object>).ToArray();
            foreach (var item in dic)
            {
                string id = (string)item["id"];
                var url = item.Where(x => x.Key == "picture").SelectMany(x => x.Value as Dictionary<string, object>).Where(x => x.Key == "data").SelectMany(x => x.Value as Dictionary<string, object>).Where(i => i.Key == "url").First().Value;
                FriendData friend = Friends.Where(x => x.FacebookID == id).FirstOrDefault();
                if (friend != null)
                    GetPictureByURL("" + url, friend);
            }
        }
        if (!string.IsNullOrEmpty(result.Error))
        {
            Debug.Log(result.Error);

        }
    }
}