c# - Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly -


i have json below:

[     {         "attributes": [             {                 "key": "name",                 "value": {                     "value": "acc 1",                     "values": [                         "acc 1"                     ]                 }             },             {                 "key": "id",                 "value": {                     "value": "1",                     "values": [                         "1"                     ]                 }             }         ],         "name": "account",         "id": "1"     },     {         "attributes": [             {                 "key": "name",                 "value": {                     "value": "acc 2",                     "values": [                         "acc 2"                     ]                 }             },             {                 "key": "id",                 "value": {                     "value": "2",                     "values": [                         "2"                     ]                 }             }         ],         "name": "account",         "id": "2"     },     {         "attributes": [             {                 "key": "name",                 "value": {                     "value": "acc 3",                     "values": [                         "acc 3"                     ]                 }             },             {                 "key": "id",                 "value": {                     "value": "3",                     "values": [                         "3"                     ]                 }             }         ],         "name": "account",         "id": "2"     } ] 

for have class below:

public class retrievemultipleresponse {     public list<attribute> attributes { get; set; }     public string name { get; set; }     public string id { get; set; } } public class value {     [jsonproperty("value")]     public string value { get; set; }     public list<string> values { get; set; } }  public class attribute {     public string key { get; set; }     public value value { get; set; } } 

i trying deserialize above json using below code:

var objresponse1 = jsonconvert.deserializeobject<retrievemultipleresponse>(jsonstr); 

but getting below error:

cannot deserialize current json array (e.g. [1,2,3]) type 'test.model.retrievemultipleresponse' because type requires json object (e.g. {"name":"value"}) deserialize correctly. fix error either change json json object (e.g. {"name":"value"}) or change deserialized type array or type implements collection interface (e.g. icollection, ilist) list can deserialized json array. jsonarrayattribute can added type force deserialize json array. path '', line 1, position 1.

your json string wrapped within square brackets ([]), hence interpreted array instead of single retrievemultipleresponse object. therefore, need deserialize type collection of retrievemultipleresponse, example :

var objresponse1 =      jsonconvert.deserializeobject<list<retrievemultipleresponse>>(jsonstr); 

Comments

Popular posts from this blog

c# - SelectList with Dictionary, add values to the Dictionary after it's assigned to SelectList -

how can i manage url using .htaccess in php? -

ios - I get the error Property '...' not found on object of type '...' -