c# - Cannot deserialize the current JSON object to generic List type -


question background:

i'm using newtonsofts json.net desearlize xml response aws service c# object strcuture.

the issue:

i'm receiving following error message when trying deserialize on imageset class category property :

cannot deserialize current json object (e.g. {"name":"value"}) type 'system.collections.generic.list`1[shoppingcomparisonengine.aws.awsrootlistpriceobject.imageset]' 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 'items.item[1].imagesets.imageset.category', line 1, position 12122. 

i should add have no control on returned xml , in turn have no control on large object structure need deserialize response into.

the code:

var awspricelostmodel = jsonconvert.deserializeobject<awslistpricerootobject>(fullyescapeddata);  

the following c# class model 'imageset'

public class imagesets {     [jsonproperty("imageset")]     public list<imageset> imageset { get; set; } }  public class imageset {     [jsonproperty("category")]     public string category { get; set; }     [jsonproperty("swatchimage")]     public swatchimage swatchimage { get; set; }     [jsonproperty("smallimage")]     public smallimage2 smallimage { get; set; }     [jsonproperty("thumbnailimage")]     public thumbnailimage thumbnailimage { get; set; }     [jsonproperty("tinyimage")]     public tinyimage tinyimage { get; set; }     [jsonproperty("mediumimage")]     public mediumimage2 mediumimage { get; set; }     [jsonproperty("largeimage")]     public largeimage2 largeimage { get; set; } } 

this screenshot showing json response category property highlighted throwing error:

enter image description here

any trying work out why there error being thrown on desearlizing list json c# appreciated.

i'm not sure raw json looks like, i'd try 1 of 2 things:

  1. use [jsonarray]attribute (docs) on imageset property.

    [jsonarray] public list<imageset> imageset { get; set; } 
  2. consider different name imageset property, or name being serialized to. you've doubled on imageset name, using represent name of class property holds list of class.

best of luck.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -