c# - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) -


what going on , how can fix it?

api -> https://developers.pipedrive.com/docs/api/v1/

public class user //related {     public int id { get; set; }     public string name { get; set; }     public string email { get; set; }     public bool has_pic { get; set; }     public string pic_hash { get; set; }     public bool active_flag { get; set; } }  public class related_objects {     public list<organization> organization { get; set; } // others things...     public list<person> person { get; set; } // others things...     public list<user> user { get; set; } }  public class registros {     public bool success { get; set; }     public virtual list<data> data { get; set; }     public virtual related_objects related_objects { get; set; }     public virtual additional_data additional_data { get; set; } } 

when returned json, looks this:

"related_objects": {     "user": {         "478548": {             "id": 478548,             "name": "username",             "email": "usernamemail",             "has_pic": false,             "pic_hash": null,             "active_flag": true         }         "478548": {             "id": 478548,             "name": "username2",             "email": "username2mail",             "has_pic": false,             "pic_hash": null,             "active_flag": true         }     } }  list<deals.data> _listregistrosdeal = new list<deals.data>(); jsontextreader jsonretorno = new jsontextreader(new stringreader(wbc.downloadstring(deals))); jsonretorno.supportmultiplecontent = true; ser.objectcreationhandling = objectcreationhandling.replace; try {     deals.registros registros = ser.deserialize<deals.registros>(jsonretorno);     foreach (deals.data reg in registros.data)     {         _listregistrosdeal.add(reg);     } } catch (jsonexception ex) {     content(ex.message); }  list<deals.data> _listregistrosdeal = new list<deals.data>(); list<deals.user> _listuser = new list<deals.user>();  jsontextreader jsonretorno = new jsontextreader(new stringreader(wbc.downloadstring(deals))); jsonretorno.supportmultiplecontent = true; var d = wbc.downloadstring(deals); ser.objectcreationhandling = objectcreationhandling.replace; try {     deals.registros registros = ser.deserialize<deals.registros>(jsonretorno);     foreach (deals.data reg in registros.data)     {         _listregistrosdeal.add(reg); // works     }     foreach (deals.user user in registros.related_objects.user)     {         _listuser.add(user);     } } catch (jsonexception ex) {     content(ex.message); } 

i've tried following tips did not solve issue:

i continue getting error + ex:

{"cannot deserialize current json object (e.g. {\"name\":\"value\"}) type 'system.collections.generic.list1[intranet.models.vendas.deals+user]' because type requires json array (e.g.[1,2,3]`) deserialize correctly.

to 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 'related_objects.user.555393', line 1, position 196632."}

newtonsoft.json.jsonexception {newtonsoft.json.jsonserializationexception}

to deserialize this:

public list<user> user { get; set; } 

your json needs array. in here:

"related_objects": {     "user": {         "478548": {             "id": 478548,             "name": "username",             "email": "usernamemail",             "has_pic": false,             "pic_hash": null,             "active_flag": true         }      "478548": {             "id": 478548,             "name": "username2",             "email": "username2mail",             "has_pic": false,             "pic_hash": null,             "active_flag": true         }     } } 

user object, not array.

to fix either write own converter change object user collection. or can change user to, example:

public dictionary<int,user> user { get; set; } 

which should deserialize fine.


Comments