ssis - 美化 json 字符串
ssis - beautify json string
我正在尝试美化一个 json 字符串,我通过以下方式获取它:
string json = JsonConvert.SerializeObject(Row, Formatting.Indented);
看起来它运行良好,但它包含 _IsNull 字段,我该如何避免?
"ID": 35208,
"ID_IsNull": false,
"name": "SONIA",
"name_IsNull": false
我会:
"ID": 35208,
"name": "SONIA"
我认为最好的方法是实现合同解析器并自动忽略所有具有 _IsNull 名称后缀的属性:
private class IgnoredNullIndicationPropertiesJsonContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.PropertyName.EndsWith("_IsNull"))
{
property.Ignored = true;
}
return property;
}
}
然后您可以像这样使用 JSON 序列化程序应用它:
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = new IgnoredNullIndicationPropertiesJsonContractResolver(),
};
string json = JsonConvert.SerializeObject(Row, Formatting.Indented, serializerSettings);
我正在尝试美化一个 json 字符串,我通过以下方式获取它:
string json = JsonConvert.SerializeObject(Row, Formatting.Indented);
看起来它运行良好,但它包含 _IsNull 字段,我该如何避免?
"ID": 35208,
"ID_IsNull": false,
"name": "SONIA",
"name_IsNull": false
我会:
"ID": 35208,
"name": "SONIA"
我认为最好的方法是实现合同解析器并自动忽略所有具有 _IsNull 名称后缀的属性:
private class IgnoredNullIndicationPropertiesJsonContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.PropertyName.EndsWith("_IsNull"))
{
property.Ignored = true;
}
return property;
}
}
然后您可以像这样使用 JSON 序列化程序应用它:
var serializerSettings = new JsonSerializerSettings
{
ContractResolver = new IgnoredNullIndicationPropertiesJsonContractResolver(),
};
string json = JsonConvert.SerializeObject(Row, Formatting.Indented, serializerSettings);