Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by kaotiklabs · Nov 06, 2013 at 11:28 AM · facebookjsonprime31parsing

Parsing Facebook graph JSON

Hi mates,

I'm trying to retrieve Facebook's friend scores using prime31 Social Plugin and have a problem deserializing a numeric value. I only want to save the retrieved numeric scores but keep getting problems. I can deserialize the strings, but keep getting errors when trying to deserialize the numeric "score" values.

I get this error in this line: double score = (((IDictionary) ht)["score"]); InvalidCastException: Cannot cast from source type to destination type.

How should I do to store the score variable? I have tried all I could imagine..., please give me some tips.

Thanks in advance!

This is my json structure.

 {
   "data": [
     {
       "user": {
         "name": "Jaume", 
         "id": "709353319"
       }, 
       "score": 20, 
       "application": {
         "name": "MiniBasket", 
         "namespace": "minibasket", 
         "id": "572013964869884"
       }
     }, 
     {
       "user": {
         "name": "Jordi", 
         "id": "639588441"
       }, 
       "score": 0, 
       "application": {
         "name": "MiniBasket", 
         "namespace": "minibasket", 
         "id": "572013964869884"
       }
     }
   ]
 }


And here is my OnLeaderboardComplete method.

 void OnLeaderboardComplete( string error, object result ) {
     
     Debug.Log("Facebook >> OnLeaderboardComplete");
     Debug.Log(" >>> result: " + result);
     Prime31.Utils.logObject( result );
                     
     var scoreResults = result as IDictionary;
     var list = scoreResults["data"] as IList;
     
     userListNames = new List<string>();
     userListScores =  new List<double>();
     
     leaderboardTotal = list.Count;
     leaderboardCount = 0;
     
     for(int i = 0; i < list.Count; i++)
     {
         var ht = list[i] as IDictionary;
         var user = ht["user"] as IDictionary;
          
                     
         double score = (((IDictionary<string, double>) ht)["score"]);            
         string id = user["id"] as string;
         string name = user["name"] as string;
         name = name.Split(' ')[0] as string;
         
         if (currentId.ToString() == id) name += " (me)";
         
         Debug.Log("id: " + id + " , name: " + name + " , score: " + score);
                 
         // store stuff
         userListNames.Add(name);
         userListScores.Add(score);    
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by Azrapse · Nov 06, 2013 at 01:01 PM

I don't totally understand what you are doing at line 7. I hope result indeed contains an IDictionary. I guess so if you say that everything works for strings.

Anyway, try changing line 22 to something like

 double score = double.Parse(ht["score"].ToString());

or even better

 double score = 0;
 double.TryParse(ht["score"].ToString(), out score);
Comment
Add comment · Show 1 · 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
avatar image kaotiklabs · Nov 06, 2013 at 07:43 PM 0
Share

Oh man, you saved my day. I had nearly tried all. Didnt try tryParse. Thanks!

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.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Parse Json Response through Dictionary object 1 Answer

I cannot log into Facebook when logged in via the settings screen of iOS 0 Answers

Unity Facebook SDK 1 Answer

Loading JSON into JSON issue,JSON file inside JSON problem 0 Answers

Failed first attempt when inviting friends using the Prime31 iOS Facebook Plugin 1 Answer


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