• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
1
Question by KNDL · May 04, 2015 at 02:45 PM · facebookjsondeserialization

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zeppike · May 04, 2015 at 02:58 PM 0
Share

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 :)

avatar image KNDL · May 04, 2015 at 03:06 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

JSON invalid value 1 Answer

Can't build for Android 0 Answers

Facebook SDK keeps asking for permissions. 2 Answers

How do I correctly install the Facebook SDK plugin? 1 Answer

Why missing android debug keystore? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges