Wednesday, 18 September 2013

JSON Object Casting Error with Facebook C# SDK for ASP.NET 3.5

JSON Object Casting Error with Facebook C# SDK for ASP.NET 3.5

I am using the Facebook C# SDK using ASP.NET 3.5 and I am getting a weird
error message for one of the calls from a sample code base provided here:
http://www.thepcwizard.in/2013/07/sample-app-using-facebook-c-sharp-sdk.html.
This example is an excellent walk-through of the basics using the Facebook
C# SDK for FB login code but I have this one issue that I can't seem to
figure out.
The error message is below:
Server Error in '/' Application.
Unable to cast object of type 'Facebook.JsonArray' to type
'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Unable to cast object of
type 'Facebook.JsonArray' to type
'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.
Source Error:
**Line 271: var athletes = (IDictionary<string,
object>)me["favorite_athletes"];**
Line 272:
Line 273: foreach (var athlete in (JsonArray)athletes)
I have compiled the FB C# SDK version 3.5 as instructed and mostly
everything is working fine. I had to replace the dynamic types to var
types and cast them to IDictionary collections because ASP.NET 3.5 does
not support dynamic types. All good. Everything works great including the
friends list generation and display and all of my personal data being
called but the favorite athletes call is causing the error (code is
below). I think the issue lies in that fact that there is a "data"
variable called for the friends lists and this "data" variable is not
called for the athletes list. This is the only difference in how the JSON
object is returned from what I can see. The code sample seems to recognize
this by removing the "data" variable when the data is retrieved but still
no luck on the error.
The favorite athletes JSON data as it comes back from Facebook is below:
{
"favorite_athletes": [
{
"id": "14185406833",
"name": "Serena Williams"
},
{
"id": "61801828075",
"name": "Venus Williams"
},
{
"id": "386180624733248",
"name": "Robert Griffin III"
},
{
"id": "164825930120",
"name": "Tiger Woods"
},
{
"id": "363865183112",
"name": "Floyd Mayweather"
},
{
"id": "142603419139653",
"name": "Michael Vick"
},
{
"id": "64637653943",
"name": "LeBron James"
}
],
"id": "8723497347"
}
The friends list data as it is returned from Facebook is below (fake user
info for the purposes of this question because I do have far more than 4
friends :) ):
{
"id": "8723497347",
"friends": {
"data": [
{
"name": "Friend 1",
"id": "1234567"
},
{
"name": "Friend 2",
"id": "567965"
},
{
"name": "Friend 3",
"id": "9847634"
},
{
"name": "Friend 4",
"id": "100005000106091"
}
],
"paging": {
"next":
"https://graph.facebook.com/8723497347/friends?limit=5000&offset=5000&__after_id=100005000106091"
}
}
}
The code block that is throwing the error is below:
private void GetUserData(string accessToken)
{
var fb = new FacebookClient(accessToken);
var me = (IDictionary<string,
object>)fb.Get("me?fields=friends,name,email,favorite_athletes");
string id = (string)me["id"]; // Store in database
string email = (string)me["email"]; // Store in database
string FBName = (string)me["name"]; // Store in database
NameText.Visible = true;
NameText.Text = FBName;
ViewState["FBName"] = FBName; // Storing User's Name in ViewState
var friends = (IDictionary<string, object>)me["friends"];
foreach (var friend in (JsonArray)friends["data"])
{
ListItem item = new
ListItem((string)(((JsonObject)friend)["name"]),
(string)(((JsonObject)friend)["id"]));
FriendList.Items.Add(item);
}
var athletes = (IDictionary<string, object>)me["favorite_athletes"];
foreach (var athlete in (JsonArray)athletes)
{
SportsPersonList.Items.Add((string)(((JsonObject)athlete)["name"]));
}
Login.Text = "Log Out";
}
Any advice on this would be appreciated! Thanks in advance for your time.

No comments:

Post a Comment