c# - Parsing JSON With Newtonsoft Throwing Incorrect Format Error -
i attempting parse json response taking response , copy/paste json2csharp adding 2 newly generated classes bottom of current class. (i not dealing multiple classes). issue having when attempt access generated class rootobject
error of
an unhandled exception of type 'newtonsoft.json.jsonserializationexception' occurred in newtonsoft.json.dll
additional information: cannot deserialize current json array (e.g. [1,2,3]) type 'test.rootobject' because type requires json object (e.g. {"name":"value"}) deserialize correctly.
even though syntax converted me, , did not change it. need alter becomes valid workable syntax?
static void main(string[] args) { string userid= "186exa"; var url = "https/?rst=" + userid; var connectionclient = new webclient(); connectionclient.headers.set("h", "xxxxxxxxxxxx"); connectionclient.headers.set("uname", "user"); connectionclient.headers.set("pswd", "pwd"); var content = connectionclient.downloadstring(url); }
edit
this class - post json shortly
public class list { public int id { get; set; } public string cmu { get; set; } public int lno { get; set; } public string clr { get; set; } public string name { get; set; } public string origin { get; set; } public string majorstyle { get; set; } public string style { get; set; } public string styleimage { get; set; } public int hid { get; set; } public string bqty { get; set; } public int cask { get; set; } public int local { get; set; } public string city { get; set; } } public class rootobject { public string style { get; set; } public list<list> list { get; set; } }
this retunred json
[{"style":"cajun","list":[{"id":1225,"cmu":"41.2","lno":10,"name":"bear","origin":"lake sinclair, mo","majorstyle":"burn yo bottom","style":"","styleimage":"","hid":1,"bqty":"1.00","cask":0,"local":0,"city":"amsterdam"}
there 2 things @ fault code have posted,
- the property "clr" not exist in json.
- the json ends prematurely, should have ]}] on end correct.
fixing both of issues, code parses correctly in newtonsoft when passing type rootobject[], per:
var o = newtonsoft.json.jsonconvert.deserializeobject<rootobject[]>(s);
where s json string.
Comments
Post a Comment