我们有一个C#控制台应用程序,可以调用多个第三方API.我们从这些API中以杨森格式获取数据.我们正在try 将SON子序列化为已定义的类.问题在于json中的"custom_attribers"属性包含具有以下 struct 的对象:

{
    ...other properties
    "custom_attributes": [
        {
            "attribute_code": "required_options",
            "value": "simple"
        },
        {
            "attribute_code": "category_ids",
            "value": [
                "2"
            ]
        },
        {
            "attribute_code": "has_options",
            "value": "0"
        }
    ]
}

正如您所看到的,"值"属性可以具有"字符串"类型或"字符串[]"类型作为值.

目前,我们有以下代码:

public class MyApiResponseClass 
{
    ... other properties
    [JsonProperty("custom_attributes")]
    public List<CustomAttribute> CustomAttributes { get; set; } = new();
}

public class CustomAttribute
{
    [JsonProperty("attribute_code")]
    public string AttributeCode { get; set; } = string.Empty;

    [JsonProperty("value")]
    public object Value { get; set; } = string.Empty;
}
var response = JsonConvert.DeserializeObject<MyApiResponseClass>(responseContent);

这会产生以下错误:

无法将当前SON数组(例如[1,2,3])反序列化为类型"System. Body",因为该类型需要一个SON基元值(例如字符串、数字、布尔值、空值)才能正确反序列化. 要修复此错误,可以将JSON更改为JSON基元值(例如字符串、数字、布尔值、空值),或者将反序列化类型更改为数组或实现集合接口(例如ICollect、IList)的类型,例如可以从JSON数组反序列化的List.JsonArrayProperty还可以添加到类型中,以强制其从杨森数组反序列化. 路径"items[0].custom_attribes [17]. value ',第1行,位置3119.'

如有任何帮助,我们将不胜感激!

推荐答案

该异常似乎是因为您正在将Value属性预初始化为string:

public object Value { get; set; } = string.Empty;

如果我删除初始值,异常就会消失:

public class CustomAttribute
{
    [JsonProperty("attribute_code")]
    public string AttributeCode { get; set; } = string.Empty;

    [JsonProperty("value")]
    public object Value { get; set; } // = string.Empty;
}

演示小提琴#1 here.

想必您不希望这样,但将房产标记为ObjectCreationHandling.Replace似乎也会让问题消失:

public class CustomAttribute
{
    [JsonProperty("attribute_code")]
    public string AttributeCode { get; set; } = string.Empty;

    [JsonProperty("value", ObjectCreationHandling = ObjectCreationHandling.Replace)]
    public object Value { get; set; } = string.Empty;
}

由于您已经使用Newtonsoft属性注释您的模型,因此这似乎是 destruct 性最小的修复方案. 演示小提琴#2 here.

最后,添加一个参数化的构造函数,该构造函数接受属性代码和值并将其标记为[JsonConstructor],也可以解决异常:

public class CustomAttribute
{
    public CustomAttribute() { }
    [JsonConstructor]
    public CustomAttribute(string attributeCode, object value) => (this.AttributeCode, this.Value) = (attributeCode, value);

    // Remainder unchanged

演示小提琴#3 here.

我无法真正解释这个例外,您可能想向Newtonsoft提出问题here.

Csharp相关问答推荐

我可以将Expressc操作设置为在另一个Expressc操作完成后自动发生吗?

将C#字符串转换为其UTF8编码字符的十六进制表示

获取Windows和Linux上的下载文件夹

如何使嵌套for-loop更高效?

需要深入了解NpgSQL DateTimeOffset处理

try 还原包时出错:Dapper已经为System.Data.SQLClient定义了依赖项''''

从c#列表中删除额外的对象&对象&>从ASP.NET WebForm返回json响应

如何在Windows 11任务调度程序中每1分钟重复一次任务?

在调整大小的控件上绘制

Blazor在FluentButton onClick事件上设置参数

Automapper 12.x将GUID映射到字符串

如何将MemberInitExpression添加到绑定中其他Lambda MemberInitExpression

VS 2022与VS 2019:如何/为什么创建额外的任务?

数据库.Migrate在对接容器重启时失败

如何在microsoft.clearscript.v8的jsondata中使用Linq

C#中类库项目的源代码生成器

如何使用实体框架核心对字符串_agg使用强制转换为varchar(Max)

Visual Studio 17.8.0制表符自动完成问题--三缩进

.NET EF Core Automapper项目到筛选不起作用

无法将.Net Framework 4.8.1升级到.Net 7