C# JSON & Parsing -
i have managed set-up simple webclient
calls wcf service in wp8 application. method fires fine , data returned via openreadcompleted
event.
what wish convert returned data in json , populate collection of objects.
this webclient
code:
private void button_click(object sender, routedeventargs e) { var webclient = new webclient(); var uri = new uri("urlgoeshere"); webclient.openreadcompleted += webclient_openreadcompleted; webclient.openreadasync(uri); }
this openreadcomplete
code:
void webclient_openreadcompleted(object sender, openreadcompletedeventargs e) { var sr = new streamreader(e.result); var data = sr.readtoend(); //todo - create collection of sightingtypes , populate sr.close(); sr.dispose(); }
and poco/object want populating:
public class sightingtype { public string name { get; set; } public string brandid { get; set; } }
update
when hover on data, can see following (shortened):
{\"message\":null,\"status\":0,\"currentversionnumber\":26,\"sightingtypes\":[{\"brandid\":\"brands\\/1\",\"destinationuserids\":[\"users\\/33\"],\"id\":\"sightingtypes\\/8\",\"isdeleted\":false,\"isenabled\":true,\"name\":\"michael johnson\"}
what particularly interested in name , brandid.
you can use json.net , following classes (your root object not sightingtype )....
var result = jsonconvert.deserializeobject<response>(data);
public class sightingtype { public string brandid { get; set; } public list<string> destinationuserids { get; set; } public string id { get; set; } public bool isdeleted { get; set; } public bool isenabled { get; set; } public string name { get; set; } } public class response { public object message { get; set; } public int status { get; set; } public int currentversionnumber { get; set; } public list<sightingtype> sightingtypes { get; set; } }
also see this site can class definitions json string
Comments
Post a Comment