原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty

 public class NullToEmptyStringResolver : DefaultContractResolver
{
/// <summary>
/// 创建属性
/// </summary>
/// <param name="type">类型</param>
/// <param name="memberSerialization">序列化成员</param>
/// <returns></returns>
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties()
.Select(x =>
{
var property = CreateProperty(x, memberSerialization);
property.ValueProvider = new NullToEmptyStringValueProvider(x);
return property;
}).ToList();
} /// <inheritdoc />
/// <summary>
/// 小写
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
protected override string ResolvePropertyName(string propertyName)
{
return propertyName.ToLower();
}
}
public class NullToEmptyStringValueProvider : IValueProvider
{
private readonly PropertyInfo _memberInfo; /// <summary>
/// 构造函数
/// </summary>
/// <param name="memberInfo"></param>
public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
{
_memberInfo = memberInfo;
} /// <inheritdoc />
/// <summary>
/// 获取Value
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public object GetValue(object target)
{
var result = _memberInfo.GetValue(target);
if (_memberInfo.PropertyType == typeof(string) && result == Convert.DBNull)
{
result = string.Empty;
} return result; } /// <inheritdoc />
/// <summary>
/// 设置Value
/// </summary>
/// <param name="target"></param>
/// <param name="value"></param>
public void SetValue(object target, object value)
{
_memberInfo.SetValue(target, value);
}
}

Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty的更多相关文章

  1. Asp.Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty

    public class NullToEmptyStringResolver : DefaultContractResolver { /// <summary> /// 创建属性 /// ...

  2. Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案

    使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...

  3. Asp.Net中使用Newtonsoft.Json转换,读取,写入

    using Newtonsoft.Json;using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象目标对象 = JsonConvert.Deserial ...

  4. Newtonsoft.Json序列化字符串-格式化

    转自:https://blog.csdn.net/wlphlj/article/details/51982866最近C#中需要将实体进行json序列化,使用了Newtonsoft.Json publi ...

  5. Newtonsoft.Json序列化字符串-格式化和时间格式问题

    最近C#中需要将实体进行json序列化,使用了Newtonsoft.Json public static void TestJson()        {            DataTable d ...

  6. 在.NET中使用Newtonsoft.Json转换,读取,写入的方法介绍

    全局引用 using Newtonsoft.Json; using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象 目标对象 = JavaScriptCon ...

  7. 在Linux下使用iconv转换字符串编码

    在Linux下写C程序,尤其是网络通信程序时经常遇到编码转换的问题,这里要用到iconv函数库. iconv函数库有以下三个函数 123456 #include <iconv.h>icon ...

  8. c# 在.NET使用Newtonsoft.Json转换,读取,写入json

    转自:http://blog.sina.com.cn/s/blog_70686f3a0101kemg.html  首先,大家要明白什么是json,了解更多关于json方面资料大家可以点击https:/ ...

  9. Newtonsoft.Json转换强类型DataTable错误:Self referencing loop detected with type ......

    问题,在使用Newtonsoft.Json对强类型的DataTable进行系列化时会出现循环引用错误 解决办法,不要直接系列化强类型的DataTable,改为 JsonConvert.Serializ ...

随机推荐

  1. ADO.net简单增删改查

    嘿嘿,又到了总结了的时间,今天我们学习了ADO.net,什么是ADO.NET:ADO.NET就是一组类库,这组类库可以让我们通过程序的方式访问数据库,就像System.IO下的类操作文件一样, Sys ...

  2. [Angular] Protect The Session Id with https and http only

    For the whole signup process. we need to Hash the password to create a password digest Store the use ...

  3. Android 监听软键盘点击回车及换行事件

    mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean ...

  4. 【CS Round #48 (Div. 2 only)】Water Volume

    [链接]h在这里写链接 [题意] 在这里写题意 [题解] 枚举0在哪个位置就好. [错的次数] 0 [反思] 在这了写反思 [代码] #include <bits/stdc++.h> us ...

  5. [D3] Select DOM Elements with D3 v4

    Before you can create dazzling data driven documents, you need to know how D3 accesses the DOM. This ...

  6. 数据结构-堆实现优先队列(java)

    队列的特点是先进先出.通常都把队列比喻成排队买东西,大家都非常守秩序,先排队的人就先买东西. 可是优先队列有所不同,它不遵循先进先出的规则,而是依据队列中元素的优先权,优先权最大的先被取出. 这就非常 ...

  7. Android LoaderManager与CursorLoader用法

    一.基本概念 1.LoaderManager LoaderManager用来负责管理与Activity或者Fragment联系起来的一个或多个Loaders对象. 每个Activity或者Fragme ...

  8. UVA 10917 Walk Through the Forest SPFA

    uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...

  9. 10.4 android输入系统_框架、编写一个万能模拟输入驱动程序、reader/dispatcher线程启动过程源码分析

    1. 输入系统框架 android输入系统官方文档 // 需FQhttp://source.android.com/devices/input/index.html <深入理解Android 卷 ...

  10. 2、JNI说明

    JNI (Java Native Interface) 1. JAVA调用CLinux是用C语言写的,可以写一个APP简单调用open,read,write来访问驱动程序;Android是用Java写 ...