0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Siddharth
  • 4,142
  • 9
  • 44
  • 90
  • I have received json information into Dictionary form, now I want to retrieve each information from response. – Siddharth Jul 31 '18 at 10:33
  • Question edited little bit too... – Siddharth Jul 31 '18 at 10:35
  • 1
    May this is duplicate of this [Getting Text from IResult Facebook SDK, 7.2.0](https://stackoverflow.com/questions/33250518/getting-text-from-iresult-facebook-sdk-7-2-0) – High Jul 31 '18 at 10:38
  • Look into the [JSON serializer](https://docs.unity3d.com/Manual/JSONSerialization.html) in Unity. That's what I would use to go from raw JSON to an object in C# that you can use. – BenjaFriend Jul 31 '18 at 13:03

2 Answers2

0

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.

LiefLayer
  • 977
  • 1
  • 12
  • 29
0

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.

Siddharth
  • 4,142
  • 9
  • 44
  • 90