skip to Main Content

I have Json response through Facebook API like this:

enter image description here

Now I want to parse this data so I can use within my game. For this purpose, I have written code up to this:

 public void OnChallengesButtonClick ()
 {
     SoundManager.Instance.PlayButtonClickSound ();

     FB.API ("/me/friends", HttpMethod.GET, RetrieveFacebookFriends);
 }

 public void RetrieveFacebookFriends (IGraphResult result)
 {
     if (!string.IsNullOrEmpty (result.Error)) {
         Debug.Log ("ERROR: " + result.Error);
         return;
     }

     IDictionary<string,object> friendsDict = result.ResultDictionary;

     Debug.Log ("raw: " + result.RawResult);
 }

So how to extract name and ID data from available Json response through Dictionary object?

2

Answers


  1. Chosen as BEST ANSWER

    I think this is all I need: JSON with Unity

    friends = (List<object>)(((Dictionary<string, object>)friendsH) ["data"]);
    if(friends.Count > 0) {
        var friendDict = ((Dictionary<string,object>)(friends[0]));
        var friend = new Dictionary<string, string>();
        friend["id"] = (string)friendDict["id"];
        friend["first_name"] = (string)friendDict["first_name"];
    }
    

    Before I can't able to find this post from Facebook.


  2. Basically you should create a class to serialize/deserialize an object:

    class FBResponse
      FBDataResponse data;
    
    class FBDataResponse
      string name;
      string id;
    

    Take a look to my response here for more details (after “Final EDIT” part):
    Converting arrays of arrays to json

    It should be easy to create those two class and use them to deserialize the FB response. Just remember that your variables will need to match the same name with the json response from Facebook (for example you cannot call data data1).

    After you have the object it should be easy to do what you want with it… and with the object you should be able to get the json again.

    PS. Unity default json serializer does not support Dictionary that’s why you need to serialize two object. If you don’t want that you should try to use a different library.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search