C# multilevel json array parsing -


i'm having trouble parsing json string. can jobject or jarray fine , use json.net's serialize method dynamic object. problem need in model object rather loose guess. format specific json same except entries few nodes missing.

the dynamic object i'm getting after parsing (from the path of exile api):

type: array isreadonly: false hasvalues: true first: {   "id": "standard",   "description": "the default game mode.",   "registerat": null,   "event": false,   "url": "http://www.pathofexile.com/forum/view-thread/71278",   "startat": "2013-01-23t21:00:00z",   "endat": null,   "rules": [] } last: {   "id": "aug 12 3h hc race",   "description": "a 3 hour hc race event prizes. see beta status forum details.",   "registerat": null,   "event": false,   "url": null,   "startat": "2012-08-11t21:00:00z",   "endat": "2012-08-12t00:00:00z",   "rules": [     {       "id": 4,       "name": "hardcore",       "description": "a character killed in hardcore moved parent league."     }   ] } count: 50 parent:  root: [   {     "id": "standard",     "description": "the default game mode.",     "registerat": null,     "event": false,     "url": "http://www.pathofexile.com/forum/view-thread/71278",     "startat": "2013-01-23t21:00:00z",     "endat": null,     "rules": []   },  <- more stuff ->   {     "id": "aug 12 3h hc race",     "description": "a 3 hour hc race event prizes. see beta status forum details.",     "registerat": null,     "event": false,     "url": null,     "startat": "2012-08-11t21:00:00z",     "endat": "2012-08-12t00:00:00z",     "rules": [       {         "id": 4,         "name": "hardcore",         "description": "a character killed in hardcore moved parent league."       }     ]   } ] next:  previous:  path: 

when try parse model class errors out when finds rules node. tried defining rules list<string>, custom rules class (also still in unused now) , string[];

using system; using system.collections.generic; using system.linq; using system.text; using system.threading.tasks; using newtonsoft.json.linq;  namespace exiledinfo.objects {        class leaguewrapper : list<league>     {         public list<league> leagues;     }     class league     {         public string id;         public string description;         public string registerat;         public string @event;         public string url;         public string startat;         public string endat;         public list<string> rules;     }     class rules : list<string>     {         public list<string> rules;     } } 

i'm guessing/hoping i'm missing obvious.

the error i'm getting:

an unhandled exception of type 'newtonsoft.json.jsonreaderexception' occurred in newtonsoft.json.dll  additional information: error reading string. unexpected token: startobject. path 'rules[0]', line 1, position 255. 

in code:

 endpoint leagues = new endpoint("http://api.pathofexile.com/leagues");              string jsonstring = api.makerequest(leagues.getrequesturl());             jarray data = jarray.parse(jsonstring);             foreach (jobject item in data.children())             {                 league l = jsonconvert.deserializeobject<league>(jsonconvert.serializeobject(item));             } 

any awesome!

i ran yesterday. issue is, model expecting list of rules since rules empty, throws. if there 1 rule , not wrapped in [], wouldn't know how add list. link breaks down , resolved issue.

http://michaelcummings.net/mathoms/using-a-custom-jsonconverter-to-fix-bad-json-results/

here code uses in tutorial:

    public class singlevaluearrayconverter<t> : jsonconverter     {         public override void writejson(jsonwriter writer, object value, jsonserializer serializer)         {             throw new notimplementedexception();         }          public override object readjson(jsonreader reader, type objecttype, object existingvalue, jsonserializer serializer)         {             object retval = new object();             if (reader.tokentype == jsontoken.startobject)             {                 t instance = (t)serializer.deserialize(reader, typeof(t));                 retval = new list<t>() { instance };             }             else if (reader.tokentype == jsontoken.startarray)             {                 retval = serializer.deserialize(reader, objecttype);             }             return retval;         }          public override bool canconvert(type objecttype)         {             return false;         }     } 

now decorate list

[jsonconverter(typeof(singlevaluearrayconverter<list>))] public list<string> rules; 

also use visualstudio -> edit -> paste special -> paste json classes. created classes based on json consuming.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -