gpt4 book ai didi

azure - 删除具有空值的属性

转载 作者:行者123 更新时间:2023-12-02 05:52:44 25 4
gpt4 key购买 nike

在我的 DocumentDb 文档中,我不想包含具有 NULL 值的属性。例如,我有以下 POCO 类(class)。

public class Person
{
[JsonProperty(PropertyName="id")]
public int PersonId {get; set;}

[JsonProperty(PropertyName="firstName")]
public string FirstName {get; set;}

[JsonProperty(PropertyName="middleName")]
public string MiddleName {get; set;}

[JsonProperty(PropertyName="lastName")]
public string LastName {get; set;}
}

有些人没有中间名,当我将某人的文档保存在我的集合中时,我不希望包含中间名。目前,没有中间名的人被保存为:

{
"id": 1234,
"firstName": "John",
"middleName": null,
"lastName": "Smith"
}

这是正常行为吗?如果不是,我如何在我的文档中包含具有 NULL 值的中间名属性?

附注所有序列化/反序列化均由 JSON.NET 处理

最佳答案

您可以在初始化 Cosmos 客户端时执行此操作,有一个类似于 JSON.Net 的序列化选项。

CosmosClient client = new CosmosClient(yourConnectionString, new CosmosClientOptions()
{
SerializerOptions = new CosmosSerializationOptions()
{
IgnoreNullValues = true,
}
});

关于azure - 删除具有空值的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com