- Home /
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);
}
}
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);
Oh man, you saved my day. I had nearly tried all. Didnt try tryParse. Thanks!
Your answer
Follow this Question
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