asp.net mvc - Can't get "likes" our of Facebook JSON data using MVC -
i'm trying display # of "likes" of user selected entity has facebook site. url that's passed in entities facebook name:
public int getlikes(string url) { string jsonstring = new webclient().downloadstring("http://graph.facebook.com/?ids=" + url); dictionary<string, dynamic> values = jsonconvert.deserializeobject<dictionary<string, dynamic>>(jsonstring); int keyvalues = values.count; int likes = values["likes"]; return likes; }
i error "likes" not found in value. it's there. below sample of json data that's returned facebook:
{ "disney": { "about": "\"it's kind of fun impossible.\" - walt disney", "category": "company", "checkins": 26, "description": "this page place our fans. however, need have rules. please aware not accept or consider unsolicited idea submissions and, also, must reserve right remove posting or other material find off-topic, inappropriate or objectionable.", "founded": "1923", "is_published": true, "talking_about_count": 543991, "username": "disney", "website": "http://disney.com", "were_here_count": 0, "id": "11784025953", "name": "disney", "link": "http://www.facebook.com/disney", "likes": 46622418, "cover": { "cover_id": "10152010506970954", "source": "http://sphotos-d.ak.fbcdn.net/hphotos-ak-prn1/t1.0-9/s720x720/1960125_10152010506970954_1312297640_n.png", "offset_y": 0, "offset_x": 0 } } }
i have tried many different things. getting , deserializing json data isn't problem haven't been able find way of elements. appreciated.
first create class mimics json string (object) structure:
public class jsonobject { public disney = new disney(); } public class disney { public string { get; set; } public string category{get;set;} public int checkins = {get;set;} ........ public int likes = {get;set;} ........ public cover = new cover(); } public class cover { public int cover_id { get;set; } ........ }
then, initialize object serializer:
jsonobject jsonob = new jsonobject(); javascriptserializer serializer = new javascriptserializer();
finally, parse jsonstring defined class:
try { jsonob = serializer.deserialize<jsonobject>(jsonstring); //viewbag.jsondecoded = "yes"; } catch (exception e) { //viewbag.jsondecoded = "no" + ", exception: " + e.message.tostring(); }
retrieving variables:
string = jsonob.disney.about; string category = jsonob.disney.category; int checkins = jsonob.disney.checkins; int likes = jsonob.disney.likes; cover cover = jsonob.disney.cover;
hope helps.
Comments
Post a Comment