ASP.NET Core Web API:将 json 负载转换为一种格式

问题描述 投票:0回答:1

JSON 中的一些属性采用驼峰命名法,而另一些属性则采用蛇形命名法,例如

re_broadcast_timestamp
corporate_actions
。因此,它无法正确绑定模型并引发错误。

这是 JSON 有效负载的示例:

{
    "announcements": {
        "data": [
            {
                "symbol": "",
                "broadcastdate": "20-Jul-2024 10:02:19",
                "subject": "Ans"
            }
        ]
    },
    "corporate_actions": {
        "reDilEPS": "16.68",
        "reProLossBefTax": "1505100",
        "proLossAftTax": "1128300",
        "re_broadcast_timestamp": "22-Apr-2024 19:40",
        "xbrl_attachment": "https://"
    }
}

我已声明 C# 属性如下:

public decimal ProLossAftTax { get; set; }
public string? ReBroadcastTimestamp { get; set; }
public string? XbrlAttachment { get; set; }
public string? NaAttachment { get; set; }

注意:我在 JSON 中有 N 个属性,使用手动绑定很难使用

System.Text.Json.Serialization

public class CorporateActions
{
    [JsonPropertyName("reDilEPS")]
    public string? ReDilEps { get; set; }

    [JsonPropertyName("reProLossBefTax")]
    public string? ReProLossBefTax { get; set; }

    [JsonPropertyName("proLossAftTax")]
    public decimal ProLossAftTax { get; set; }

    [JsonPropertyName("re_broadcast_timestamp")]
    public string? ReBroadcastTimestamp { get; set; }

    [JsonPropertyName("xbrl_attachment")]
    public string? XbrlAttachment { get; set; }
}

public class AnnouncementData
{
    [JsonPropertyName("symbol")]
    public string? Symbol { get; set; }

    [JsonPropertyName("broadcastdate")]
    public string? BroadcastDate { get; set; }

    [JsonPropertyName("subject")]
    public string? Subject { get; set; }
}

我尝试在

Startup
文件中添加以下代码:

builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});

这是我需要序列化并传递给控制器的例外 JSON 结果(没有

under_score
),或者如果在发送到控制器之前它是任何其他格式,则应将其转换为一种正确的格式。

"announcements": {
    "data": 
        [
            {
                "symbol": "RELIANCE",
                "broadcastdate": "20-Jul-2024 10:02:19",
                "subject": "Anates"
            }
        ]
},
"corporateactions": {
    "redilEPS": "16.68",
    "reproLossBefTax": "1505100",
    "proLossAftTax": "1128300",
    "rebroadcasttimestamp": "22-Apr-2024 19:40",
    "xbrlattachment": "https://"
}]
json asp.net-core asp.net-core-webapi .net-6.0
1个回答
0
投票

要解决此问题,请创建一个可以处理

Camel Case
Snake Case
属性的自定义 JsonNamingPolicy JsonNamingPolicy

public class SnakeCaseNamingPolicy : JsonNamingPolicy
{
    public override string ConvertName(string name)
    {
        return string.Join("", name.Split('_')
            .Select((word, index) =>
            index == 0 ? word : char.ToUpperInvariant(word[0]) + word.Substring(1)));
    }
}

在你的startup.cs或program.cs中

builder.Services.AddControllers().AddJsonOptions(options =>
{
    options.JsonSerializerOptions.PropertyNamingPolicy = new SnakeCaseNamingPolicy();
    options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
});
© www.soinside.com 2019 - 2024. All rights reserved.