json to c# deserialization with json.net -


i know there has been countless posts json deserialization, have followed of posts answers, , haven't worked out. think main issue can't seem wrap head around how json built structure-wise.

i have following json string api:

{   "totalcount_str": "3",   "items": [     {       "standing": 10,       "corporation": {         "name": "borkedlabs",         "isnpc": false,         "href": "https://crest-tq.eveonline.com/corporations/98046548/",         "id_str": "98046548",         "logo": {           "32x32": {             "href": "http://imageserver.eveonline.com/corporation/98046548_32.png"           },           "64x64": {             "href": "http://imageserver.eveonline.com/corporation/98046548_64.png"           },           "128x128": {             "href": "http://imageserver.eveonline.com/corporation/98046548_128.png"           },           "256x256": {             "href": "http://imageserver.eveonline.com/corporation/98046548_256.png"           }         },         "id": 98046548       },       "href": "https://crest-tq.eveonline.com/characters/94512721/contacts/98046548/",       "contact": {         "id_str": "98046548",         "href": "https://crest-tq.eveonline.com/corporations/98046548/",         "name": "borkedlabs",         "id": 98046548       },       "contacttype": "corporation"     },     {       "standing": 10,       "character": {         "name": "xxxx yyyy",         "corporation": {           "name": "xxyshs",           "isnpc": false,           "href": "https://crest-tq.eveonline.com/corporations/98401169/",           "id_str": "98401169",           "logo": {             "32x32": {               "href": "http://imageserver.eveonline.com/corporation/98401169_32.png"             },             "64x64": {               "href": "http://imageserver.eveonline.com/corporation/98401169_64.png"             },             "128x128": {               "href": "http://imageserver.eveonline.com/corporation/98401169_128.png"             },             "256x256": {               "href": "http://imageserver.eveonline.com/corporation/98401169_256.png"             }           },           "id": 98401169         },         "isnpc": false,         "href": "https://crest-tq.eveonline.com/characters/95161569/",         "capsuleer": {           "href": "https://crest-tq.eveonline.com/characters/95161569/capsuleer/"         },         "portrait": {           "32x32": {             "href": "http://imageserver.eveonline.com/character/95161569_32.jpg"           },           "64x64": {             "href": "http://imageserver.eveonline.com/character/95161569_64.jpg"           },           "128x128": {             "href": "http://imageserver.eveonline.com/character/95161569_128.jpg"           },           "256x256": {             "href": "http://imageserver.eveonline.com/character/95161569_256.jpg"           }         },         "id": 95161569,         "id_str": "95161569"       },       "contact": {         "id_str": "95161569",         "href": "https://crest-tq.eveonline.com/characters/95161569/",         "name": "xxxx yyyy",         "id": 95161569       },       "href": "https://crest-tq.eveonline.com/characters/94512769/contacts/95161569/",       "contacttype": "character",       "watched": false,       "blocked": false     },     {       "standing": -10,       "alliance": {         "id_str": "99000003",         "href": "http://crest.regner.dev/alliances/99000003/",         "id": 99000003,         "name": "one 1 corporation alliance"       },       "href": "http://crest.regner.dev/characters/90000001/contacts/99000003/",       "contact": {         "id_str": "99000003",         "href": "http://crest.regner.dev/alliances/99000003/",         "name": "one 1 corporation alliance",         "id": 99000003       },       "contacttype": "alliance"     }   ],   "pagecount": 1,   "pagecount_str": "1",   "totalcount": 3 } 

note items array can contain number of "contacts".

by using http://json2csharp.com/ have converted classes in c# following:

public class contacts {     public string totalcount_str { get; set; }     public item[] items { get; set; }     public int pagecount { get; set; }     public string pagecount_str { get; set; }     public int totalcount { get; set; } }  public class item {     public int standing { get; set; }     public alliance alliance { get; set; }     public string href { get; set; }     public contact contact { get; set; }     public string contacttype { get; set; }     public character character { get; set; }     public bool watched { get; set; }     public bool blocked { get; set; } }  public class alliance {     public string id_str { get; set; }     public string href { get; set; }     public int id { get; set; }     public string name { get; set; } }  public class contact {     public string id_str { get; set; }     public string href { get; set; }     public string name { get; set; }     public int id { get; set; } }  public class character {     public string name { get; set; }     public corporation corporation { get; set; }     public bool isnpc { get; set; }     public string href { get; set; }     public capsuleer capsuleer { get; set; }     public portrait portrait { get; set; }     public int id { get; set; }     public string id_str { get; set; } }  public class corporation {     public string name { get; set; }     public bool isnpc { get; set; }     public string href { get; set; }     public string id_str { get; set; }     public logo logo { get; set; }     public int id { get; set; } }  public class logo {     public _32x32 _32x32 { get; set; }     public _64x64 _64x64 { get; set; }     public _128x128 _128x128 { get; set; }     public _256x256 _256x256 { get; set; } }  public class _32x32 {     public string href { get; set; } }  public class _64x64 {     public string href { get; set; } }  public class _128x128 {     public string href { get; set; } }  public class _256x256 {     public string href { get; set; } }  public class capsuleer {     public string href { get; set; } }  public class portrait {     public _32x32 _32x32 { get; set; }     public _64x64 _64x64 { get; set; }     public _128x128 _128x128 { get; set; }     public _256x256 _256x256 { get; set; } } 

and trying deserialize with:

list<contacts> templist = newtonsoft.json.jsonconvert.deserializeobject<list<contacts>>(response.content); 

i appreciate me on right tracks. error when trying way:

cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[contactwatchlister.models.contacts]' because type requires json array (e.g. [1,2,3]) deserialize correctly. fix error either change json json array (e.g. [1,2,3]) or change deserialized type normal .net type (e.g. not primitive type integer, not collection type array or list<t>) can deserialized json object. jsonobjectattribute can added type force deserialize json object. path 'totalcount_str', line 1, position 18. 

i stuck don't why having trouble understanding "totalcount_str" string.

i have tried use [jsonproperty("attribute")] on attributes in classes model correct, same error.

i'm pretty sure doing rather simple, wrong, can't wrap head around it. hope can help! :-)

if response.content contains json string described think statement should be:

var templist = newtonsoft.json.jsonconvert.deserializeobject<contacts>(response.content); 

your json has "totalcount_str", items, pagecount, pagecount_str , totalcount fields in outermost "level", can deserialized class has properties (or properties correct jsonattributes correspond fieldnames).

the 1 tried work following json, outermost entity array:

[   {     "totalcount_str": "3",     "items": [       {         "standing": 10,         "corporation": {           "name": "borkedlabs",           "isnpc": false,           "href": "https://crest-tq.eveonline.com/corporations/98046548/",           "id_str": "98046548",           "logo": {             "32x32": {               "href": "http://imageserver.eveonline.com/corporation/98046548_32.png"             },             "64x64": {               "href": "http://imageserver.eveonline.com/corporation/98046548_64.png"             },             "128x128": {               "href": "http://imageserver.eveonline.com/corporation/98046548_128.png"             },             "256x256": {               "href": "http://imageserver.eveonline.com/corporation/98046548_256.png"             }           },           "id": 98046548         },         "href": "https://crest-tq.eveonline.com/characters/94512721/contacts/98046548/",         "contact": {           "id_str": "98046548",           "href": "https://crest-tq.eveonline.com/corporations/98046548/",           "name": "borkedlabs",           "id": 98046548         },         "contacttype": "corporation"       },       {         "standing": 10,         "character": {           "name": "xxxx yyyy",           "corporation": {             "name": "xxyshs",             "isnpc": false,             "href": "https://crest-tq.eveonline.com/corporations/98401169/",             "id_str": "98401169",             "logo": {               "32x32": {                 "href": "http://imageserver.eveonline.com/corporation/98401169_32.png"               },               "64x64": {                 "href": "http://imageserver.eveonline.com/corporation/98401169_64.png"               },               "128x128": {                 "href": "http://imageserver.eveonline.com/corporation/98401169_128.png"               },               "256x256": {                 "href": "http://imageserver.eveonline.com/corporation/98401169_256.png"               }             },             "id": 98401169           },           "isnpc": false,           "href": "https://crest-tq.eveonline.com/characters/95161569/",           "capsuleer": {             "href": "https://crest-tq.eveonline.com/characters/95161569/capsuleer/"           },           "portrait": {             "32x32": {               "href": "http://imageserver.eveonline.com/character/95161569_32.jpg"             },             "64x64": {               "href": "http://imageserver.eveonline.com/character/95161569_64.jpg"             },             "128x128": {               "href": "http://imageserver.eveonline.com/character/95161569_128.jpg"             },             "256x256": {               "href": "http://imageserver.eveonline.com/character/95161569_256.jpg"             }           },           "id": 95161569,           "id_str": "95161569"         },         "contact": {           "id_str": "95161569",           "href": "https://crest-tq.eveonline.com/characters/95161569/",           "name": "xxxx yyyy",           "id": 95161569         },         "href": "https://crest-tq.eveonline.com/characters/94512769/contacts/95161569/",         "contacttype": "character",         "watched": false,         "blocked": false       },       {         "standing": -10,         "alliance": {           "id_str": "99000003",           "href": "http://crest.regner.dev/alliances/99000003/",           "id": 99000003,           "name": "one 1 corporation alliance"         },         "href": "http://crest.regner.dev/characters/90000001/contacts/99000003/",         "contact": {           "id_str": "99000003",           "href": "http://crest.regner.dev/alliances/99000003/",           "name": "one 1 corporation alliance",           "id": 99000003         },         "contacttype": "alliance"       }     ],     "pagecount": 1,     "pagecount_str": "1",     "totalcount": 3   } ] 

Comments