简体   繁体   English

无法将当前JSON对象反序列化为通用List类型

[英]Cannot deserialize the current JSON object to generic List type

Question Background: 问题背景:

I'm using Newtonsofts JSON.NET to desearlize an XML response from a AWS service to a C# object strcuture. 我正在使用Newtonsofts JSON.NET取消从AWS服务到C#对象结构的XML响应的学习。

The Issue: 问题:

I'm receiving the following error message when trying to deserialize on the ImageSet class Category property : 尝试在ImageSetCategory属性上反序列化时,我收到以下错误消息:

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. 我应该补充一点,我无法控制返回的XML,也无法控制需要反序列化到的大型对象结构。

The Code: 编码:

var awsPriceLostModel = JsonConvert.DeserializeObject<AwsListPriceRootObject>(fullyEscapedData); 

The following is the C# class model for 'ImageSet' 以下是“ ImageSet”的C#类模型

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: 此屏幕快照显示了突出显示Category属性的JSON响应,该错误引发错误:

在此处输入图片说明

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. 任何试图弄清为什么将列表从JSON反序列化为C#都会引发错误的帮助将不胜感激。

I'm not sure what your raw JSON looks like, but I'd try one of two things: 我不确定您的原始JSON是什么样的,但是我会尝试以下两种方法之一:

  1. Use the [JsonArray] attribute (docs) on your ImageSet property. 在ImageSet属性上使用[JsonArray]属性(docs)

     [JsonArray] public List<ImageSet> ImageSet { get; set; } 
  2. Consider a different name for your ImageSet property, or the name that it is being serialized to. 考虑ImageSet属性的其他名称,或将其序列化为的名称。 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. 您已将ImageSet名称加倍,使用它来表示类的名称以及保存该类列表的属性。

Best of luck. 祝你好运。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将当前JSON对象反序列化为类型&#39;System.Collections.Generic.List - Cannot deserialize the current JSON object into type 'System.Collections.Generic.List 无法反序列化当前JSON对象(例如{“ name”:“ value”})为类型&#39;System.Collections.Generic.List - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List 无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections.Generic.List`1 - Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1 无法反序列化当前JSON对象..以键入&#39;System.Collections.Generic.List`1 [System.String]&#39; - Cannot deserialize the current JSON object..into type 'System.Collections.Generic.List`1[System.String]' 无法将当前JSON对象(例如{“ name”:“ value”})反序列化为类型&#39;System.Collections.Generic.List`1&#39; - Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1' Cannot deserialize the current JSON object into type 'System.Collections.Generic.List1' in Xamarin Forms - Cannot deserialize the current JSON object into type 'System.Collections.Generic.List1' in Xamarin Forms 无法将JSON对象反序列化为“System.Collections.Generic.List”类型 - Cannot deserialize JSON object into type 'System.Collections.Generic.List' JSON.Net - 无法将当前json对象(例如{“name”:“value”})反序列化为类型&#39;system.collections.generic.list`1 - JSON.Net - cannot deserialize the current json object (e.g. {“name”:“value”}) into type 'system.collections.generic.list`1 错误:无法将当前 JSON object(例如 {“name”:“value”})反序列化为类型 'System.Collections.Generic.List`1 - Error: Cannot deserialize the current JSON object (e.g. {“name”:“value”}) into type 'System.Collections.Generic.List`1 JsonSerializationException:无法将当前 JSON object(例如 {"name":"value"})反序列化为类型 'System.Collections。 - JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM