c# - ssis - 美化 json 字符串
问题描述
我正在尝试美化一个 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);
推荐阅读
- jenkins - 如何在不知道名称(或编号)的情况下从共享库中的资源文件夹加载文件?
- javascript - 对引导表中的数据进行排序(数据字段问题)
- javascript - 节点 Ramda + 根据值查找对象
- c++ - 在 C++ 中,变量声明左侧的大括号是什么意思?
- python - 在不使用枚举的情况下查找匹配模式的行号
- ios - 我如何在 Swift 3 中处理字典
- php - gcloud sql连接不起作用
- ios - IOS如何在swift中获取多个String.range(of:string)
- java - Lucene 6.2.1 如何在不知道其名称的情况下获取所有字段名称或搜索所有字段
- javascript - 更改数据源时Angularjs Devextreme组件不刷新