System.Text.Json 不骆驼化动态 属性 的属性

System.Text.Json does not camelize properties of the dynamic property

我正在使用 ASP.NET Core 和 System.Text.Json

这是我的示例操作方法:

    [HttpGet]
    public object Person()
    {
        dynamic relatedItems = new ExpandoObject();
        relatedItems.LastName = "Last";
        var result = new 
        {
            FirstName = "First",
            RelatedItems = relatedItems
        };
        return result;
    }

这是我得到的回复:

{
    firstName: "First",
    relatedItems: {
        LastName: "Last"
    }
}

如您所见,LastName 是动态 属性 的 属性,没有驼峰化。

我怎样才能把所有的东西都return变成驼峰式的?

更新。那个答案不是我的答案。如您所见,我已经将 firstName 属性 正确驼峰化了。

ExpandoObject 将被视为字典,因此除了 PropertyNamingPolicy:

之外还需要设置 DictionaryKeyPolicy
dynamic relatedItems = new ExpandoObject();
relatedItems.LastName = "Last";
var result = new 
{
    FirstName = "First",
    RelatedItems = relatedItems
};
var s=JsonSerializer.Serialize(result, new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    DictionaryKeyPolicy = JsonNamingPolicy.CamelCase
});
Console.WriteLine(s); // prints {"firstName":"First","relatedItems":{"lastName":"Last"}}