【问题标题】:Facebook deserializingFacebook 反序列化
【发布时间】:2016-03-26 02:08:11
【问题描述】:

我需要帮助反序列化我从 facebook 返回的 JSON。

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

当我尝试解析 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 "";
    }
}

【问题讨论】:

  • friendList = (List)(dict["data"]);这个。不正确的铸造。有很多方法可以投射它,但这是不正确的。使用 Linq 会很容易。 dict.ToList&lt;KeyValuePair&lt;string, object&gt;&gt;();

标签: json facebook unity3d


【解决方案1】:

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

注意没有测试错别字。在 SO 中直接在这里输入

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
}

【讨论】:

    猜你喜欢
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-09
    相关资源
    最近更新 更多