- Home /
Deserialize Facebook friends result
I'm trying to deserialize the result that returned from FB request.
I've managed to get the users list who are already logged in to my app through facebook by using this line:
FB.API ("/me/friends", Facebook.HttpMethod.GET, FBFriendsCallback);
After a quick debugging I can see that the recieved data is fine including the usernames, IDs etc..
The only issue that holding me is that I can't figure out how to deserialize the FBResult.
Here's what I've tried (the DeserializeJSONFriends function is taken from one of Facebook examples):
public static List<object> DeserializeJSONFriends(string response)
{
Dictionary<string, object> responseObject = Json.Deserialize(response) as Dictionary<string, object>;
object friendsH;
List<object> friends = new List<object>();
if (responseObject.TryGetValue("invitable_friends", out friendsH))
{
friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
}
if (responseObject.TryGetValue("friends", out friendsH))
{
friends.AddRange((List<object>)(((Dictionary<string, object>)friendsH)["data"]));
}
return friends;
}
I keep getting 0 count from the friends object although the original result that sent to this function is totally fine including all the users details.
*none of the "if"s is true at runtime so none of this objects getting values.
Deserializing complex JSON manually is a huge pain in the ass. I wonder if there is a ParseJSONFriends method? If there isn't I would try to chop the script after the first Deserialize and Log the count. If it is still 0 I would switch to $$anonymous$$iniJSON or something I know better :)
After the first deserialize the count is 3 actually, and I think it should be only 1 since I'm testing it with 1 user only. Anyways it's not 0 so I guess one of the "if" statements is wrong
Answer by KNDL · May 04, 2015 at 04:12 PM
I've finally managed to get the values with alot of help from this question.
I'm adding my code, hopefully it'll help someone at the future.
public void FriendsHighscoreHndlr (string FBresult){
var dict = Json.Deserialize(FBresult) as Dictionary<string,object>;
var friendList = new List<object>();
friendList = (List<object>)(dict["data"]);
int _friendCount = friendList.Count;
Debug.Log("Found friends on FB, _friendCount ... " +_friendCount);
List<string> friendIDsFromFB = new List<string>();
for (int i=0; i<_friendCount; i++) {
string friendFBID = getDataValueForKey( (Dictionary<string,object>)(friendList[i]), "id");
string friendName = getDataValueForKey( (Dictionary<string,object>)(friendList[i]), "name");
Debug.Log( i +"/" +_friendCount +" " +friendFBID +" " +friendName);
friendIDsFromFB.Add(friendFBID);
}
}
getDataValueForKey
private string getDataValueForKey(Dictionary<string, object> dict, string key) {
object objectForKey;
if (dict.TryGetValue(key, out objectForKey)) {
return (string)objectForKey;
} else {
return "";
}
}
I have to mention that some extra documentation about deserialization from Facebook and Unity side will be definitely helpful.