Deserialize Json likes from friends of facebook asp.net mvc -
i need deserialize json string facebook graph:
{ "id": "1741240583", "music": { "data": [ { "name": "kmn | kill name", "id": "168949476496447", "created_time": "2013-05-01t07:30:54+0000" }, { "name": "hocus pocus", "id": "174462922710692", "created_time": "2013-04-16t17:55:46+0000" } ] } }
the way have did this:
public class result { public music music { get; set; } } public class music { public data[] data { get; set; } } public class data { public string[] name { get; set; } } protected void button1_click(object sender, eventargs e) { string teste = "{\"id\": \"723560709\",\"music\": {\"data\": [{\"name\": \"loko ( life opium kill on )\",\"id\": \"129518483779162\",\"created_time\": \"2013-05-07t02:54:39+0000\"},],}}"; result soap = jsonconvert.deserializeobject<result>(teste);
but returns error:
cannot deserialize current json array (e.g. [1,2,3]) type 'guiadepresentes.buscape.buscape_controle+data' because type requires json object (e.g. {"name":"value"}) deserialize correctly. fix error either change json json object (e.g. {"name":"value"}) or change deserialized type array or type implements collection interface (e.g. icollection, ilist) list<t> can deserialized json array. jsonarrayattribute can added type force deserialize json array. path 'music.data', line 1, position 38.
how can make thing work?
ps:yeah, newbie in programming :d
i believe json test string not formed correctly, have checked on http://jsonlint.com/ , proves invalid.
now, had big issues working json website helped me lot http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
hope helps :)
test example
using system.web.script.serialization; public class datum { public string name { get; set; } public string id { get; set; } public string created_time { get; set; } } public class paging { public string next { get; set; } } public class music { public list<datum> data { get; set; } public paging paging { get; set; } } public class rootobject { public string id { get; set; } public music music { get; set; } } protected void page_load(object sender, eventargs e) { webclient wc = new webclient(); javascriptserializer json = new javascriptserializer(); string _token = "baacedeose0cbae8u2tlen51zbd8g8nbsupvinb0dyfc2wfsgmbp2x8vl6zaynv81ixdzb0pkzcffscjgjlvg8zsj1fmsi6e7al5smludpza5zcr3rwjjjduyxx3sdluiaq1bj8hgjrkpyvw6petp96mdmwf2vlnrivcrofvqlo4yneqpj23rb5gv7gwrysbvnplksg0dhags79iap4zbzaarwuvpsbnfzbcwzd"; string _url = "https://graph.facebook.com/yourid?fields=music.fields(name)&access_token=" + _token; string _json = wc.downloadstring(_url); rootobject jsonobject= json.deserialize<rootobject>(_json); response.write(jsonobject.music.data.first().name); response.write(_json); }
once have jsonobject can use linq access properties , data , can convert result if want too.
you can use website create classes json http://json2csharp.com/
Comments
Post a Comment