How to deserialize json string NOT to List<object> in c# -
i'm working vk.com api, in particular json string:
{ "response": [338775, {"aid":108787020, "owner_id":2373452, "artist":" moby", "title":"flowers", "duration":208, "url":"https:\/\/cs1-50v4.vk-cdn.net\/p3\/c762273870cc49.mp3?extra=t9i-rmkslahkhe8jtouuzbtzqkfve9mj_q-tpmohxphtfhazqweybf4lqroy64xlx9auzakwvlo4pecsfihywm53wmdwvcbazvt5jlibz9x8ag","lyrics_id":"6060508", "genre":22} ] } i have class parsing data:
public class albumresponse { [jsonproperty("artist")] public string artist { get; set; } [jsonproperty("title")] public string title { get; set; } [jsonproperty("duration")] public string duration { get; set; } [jsonproperty("url")] public string url { get; set; } } and list deserialization:
public class vkalbum { public list<albumresponse> response { get; set; } } than use
var album = jsonconvert.deserializeobject<vkalbum>(responsetext); but doesn't work (a first chance exception of type 'newtonsoft.json.jsonserializationexception') because of "338775" after "response". how can deserialize without using
public list<object> response { get; set; } instead of albumresponse class?
a primitive json sanitation disposal. not elegant code, i'm sure can take here.
responsetext = regex.replace(responsetext, "[\t|\r\n]", ""); if (responsetext.indexof("response\": [") != -1) { int start = responsetext.indexof('[') + 1; int end = responsetext.indexof(',', start); responsetext = responsetext.substring(0, start) + responsetext.substring(end + 1); } var album = jsonconvert.deserializeobject<vkalbum>(responsetext);
Comments
Post a Comment