Question Background:
I'm using Newtonsofts JSON.NET to desearlize an XML response from a AWS service to a C# object strcuture.
The Issue:
I'm receiving the following error message when trying to deserialize on the ImageSet
class Category
property :
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ShoppingComparisonEngine.AWS.AWSRootListPriceObject.ImageSet]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.
Path 'Items.Item[1].ImageSets.ImageSet.Category', line 1, position 12122.
I should add I have no control over the returned XML and in turn have no control over the large object structure I need to deserialize the response into.
The Code:
var awsPriceLostModel = JsonConvert.DeserializeObject<AwsListPriceRootObject>(fullyEscapedData);
The following is the C# class model for '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 a screenshot showing the JSON response with the Category
property highlighted that is throwing the error:
Any help trying to work out why there is an error being thrown on desearlizing the list from JSON to C# will be much appreciated.
答案 0 :(得分:0)
I'm not sure what your raw JSON looks like, but I'd try one of two things:
Use the [JsonArray]
attribute (docs) on your ImageSet property.
[JsonArray]
public List<ImageSet> ImageSet { get; set; }
Consider a different name for your ImageSet property, or the name that it is being serialized to. You've doubled up on the ImageSet
name, using it represent the name of the class as well as the property that holds a list of that class.
Best of luck.