Facebook反序列化

时间:2016-03-26 02:08:12

标签: json facebook unity3d

我需要帮助反序列化从Facebook回来的JSON。

我一直在尝试多种方法来解析它,但没有成功。 我似乎唯一解析的是拥有高分的朋友数量,即2:

enter image description here

当我尝试解析json中人员的姓名和分数时,就出现了问题。

InvalidCastException: Cannot cast from source type to destination type.
I/Unity   (21869):   at FacebookScript.GETCallback (IGraphResult result) [0x00000] in <filename unknown>:0
I/Unity   (21869):   at Facebook.Unity.AsyncRequestString+<Start>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0

我收到的原始结果(从logcat看到):

Raw:{"data":[{"score":60,"user":{"name":"JOHNY JOHN","id":"0000000000000"}},{"score":50,"user":{"name":"JOHN JOHN","id":"0000000000000"}}]}

这是我的代码:

public void GETCallback(IGraphResult result)
    {
        if (result.ResultDictionary != null)
        {
            Debug.Log("Raw:" + result.RawResult);

            var dict = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
            var friendList = new List<object>();
            friendList = (List<object>)(dict["data"]);

            int _friendCount = friendList.Count;
            Debug.Log("Items found:" + _friendCount);

            List<string> friendIDsFromFB = new List<string>();
            /*for (int i = 0; i < _friendCount; i++) // Tried this, same error.
            {

                foreach(KeyValuePair<string, object> entry in friendList)
                {
                    Debug.Log(entry.Key + "|" + entry.Value);
                }

                 string friendFBID = getDataValueForKey((Dictionary<string, object>)(friendList[i]), "id");
                string friendName = getDataValueForKey((Dictionary<string, object>)(friendList[i]), "name");

                Debug.Log(i + "/" + _friendCount + "|" + friendFBID +"|"+ friendName);
                NPBinding.UI.ShowToast(i + "/" + _friendCount + "|" + friendFBID + "|" + friendName, VoxelBusters.NativePlugins.eToastMessageLength.LONG);

                //friendIDsFromFB.Add(friendFBID);
            }*/

            foreach(KeyValuePair<string, object> entry in friendList) // Tried this, same error.
            {
                 Debug.Log(entry.Key + "|" + entry.Value);

            }


        }
        else
        {
            NPBinding.UI.ShowToast("result.ResultDictionary is null", VoxelBusters.NativePlugins.eToastMessageLength.LONG);
        }
    }

private string getDataValueForKey(Dictionary<string, object> dict, string key)
{
    object objectForKey;
    if (dict.TryGetValue(key, out objectForKey))
    {
        return (string)objectForKey;
    }
    else {
        return "";
    }
}

1 个答案:

答案 0 :(得分:3)

我假设您使用的是MiniJSON(至少是FB SDK附带的版本)

N.B。没有测试错别字。直接在这里打字

var dict = Json.Deserialize(result.RawResult) as Dictionary<string, object>;
var datas = (List<object>)dict["data"];
foreach(var iterator in datas) {
    var data = iterator as Dictionary<string, object>;
    Debug.Log("Score is :: "+data["score"]);  
    //var score = int.Parse((string)data["score"]);  //Parse to int after casting to string if you want the value

    var userData = data["user"] as Dictionary<string, object>;
    Debug.Log("Name is :: "+userData["name"]);
    Debug.Log("ID is :: "+userData["id"]);
    //var name = (string)userData["name"];          //Get the name
    //var id = (string)userData["id"];              //...and the ID
}