解码Facebook MiniJson

时间:2015-03-18 18:48:50

标签: c# json facebook unity3d

我正在尝试从Facebook Api的Json响应中提取Picture url。

我得到了这个结果。文本

{"name":"John Wheeler",
"id":"1374317669554451",
"picture":{
    "data":{
        "is_silhouette":false,
            "url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xpf1\/v\/t1.0-1\/p50x50\/10930189_1374295206223364_7603927373095864161_n.jpg?oh=1f1a16fdd68dd9aa4ccc141d8ad38ed4&oe=55B344AC&__gda__=1437883587_e8970bb6ff145728e14118a180e5b40a"}}}

这是尝试提取信息的代码

//Deserializing JSON returned from server
    Dictionary<string, object> JSON = Json.Deserialize(result.Text) as Dictionary<string, object>;

    List<object> data = JSON["data"] as List<object>;
    //Loop to traverse and process all the items returned from the server.
    for (int i = 0; i < data.Count; i++)
    {
        string id = System.Convert.ToString(((Dictionary<string, object>)data[i])["id"]);
        string name = System.Convert.ToString(((Dictionary<string, object>)data[i])["name"]);
        Dictionary<string, object> picInfo = ((Dictionary<string, object>)data[i])["picture"] as Dictionary<string, object>;
        string url = Util.DeserializePictureURLObject(picInfo);

    }

我收到错误KeyNotFoundException:字典中没有给定的密钥。

约翰

1 个答案:

答案 0 :(得分:1)

您的代码与显示的JSON不匹配。您的代码假定存在引用数组的顶级属性"data",但事实并非如此。您的JSON不包含任何数组,只包含一系列属性picture - &gt; data - &gt; url,如此:

{
   "name":"John Wheeler",
   "id":"1374317669554451",
   "picture":{
      "data":{
         "is_silhouette":false,
         "url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xpf1\/v\/t1.0-1\/p50x50\/10930189_1374295206223364_7603927373095864161_n.jpg?oh=1f1a16fdd68dd9aa4ccc141d8ad38ed4&oe=55B344AC&__gda__=1437883587_e8970bb6ff145728e14118a180e5b40a"
      }
   }
}

因此,要提取此信息,您必须执行以下操作:

        var url = obj.ToDict()["picture"].DeserializePictureURLObject();
        var name = System.Convert.ToString(obj.ToDict()["name"]);
        var id = System.Convert.ToString(obj.ToDict()["id"]);

使用扩展方法:

public static class Util
{
    public static IDictionary<string, object> ToDict(this object jsonObject)
    {
        return (IDictionary<string, object>)jsonObject;
    }

    public static string DeserializePictureURLObject(this object pictureObj)
    {
        // Adapted from http://answers.unity3d.com/questions/921336/found-my-highscore-on-facebook.html?sort=oldest
        var picture = pictureObj.ToDict()["data"].ToDict();
        object urlH = null;
        if (picture.TryGetValue("url", out urlH))
        {
            return (string)urlH;
        }
        return null;
    }
}

您的解析代码适用于JSON,如下所示:

{
   "data":[
      {
         "name":"John Wheeler",
         "id":"1374317669554451",
         "picture":{
            "data":{
               "is_silhouette":false,
               "url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-xpf1\/v\/t1.0-1\/p50x50\/10930189_1374295206223364_7603927373095864161_n.jpg?oh=1f1a16fdd68dd9aa4ccc141d8ad38ed4&oe=55B344AC&__gda__=1437883587_e8970bb6ff145728e14118a180e5b40a"
            }
         }
      }
   ]
}