John Wheeler
John Wheeler

Reputation: 30

Decode Facebook MiniJson

I am trying to extract the Picture url from the Json response from Facebook Api.

I get this result.Text

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

and this is the code to try to extract the info

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

    }

I get an error KeyNotFoundException: The given key was not present in the dictionary.

John

Upvotes: 0

Views: 314

Answers (1)

dbc
dbc

Reputation: 117076

Your code doesn't match the JSON shown. Your code assumes that there is a top-level property "data" that refers to an array, however this is not the case. Your JSON contains no arrays, merely a chain of properties picture -> data -> url, like so:

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

Thus, to extract this information, you must do:

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

Using the extension methods:

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

Your parsing code would work for JSON that looks like this:

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

Upvotes: 1

Related Questions