json null
{
"ResourceId": 0,
"JsonKey": "Account",
"GroupId": 21,
"Locale": "en-US",
"JsonValue": {
"Columns": {
"2": "2",
"Title": "343434"
},
"Labels": {},
"Others": {},
"Tips": {}
}
}
第一种:使用对象的字段属性设置JsonProperty来实现(不推荐,因为需要手动的修改每个字段的属性)
|
1
2
3
4
5
6
7
|
public class UserInfo{ [JsonProperty("id")] public int Id{ set; get; } [JsonProperty("userName")] public string UserName{ set; get; }} |
第二种:使用newtonsoft.json来设置格式化的方式(推荐使用)
|
1
2
3
4
5
6
7
|
var user = new { Name = "john", Age = 19 }; var serializerSettings = new JsonSerializerSettings { // 设置为驼峰命名 ContractResolver = new CamelCasePropertyNamesContractResolver() };var userStr = JsonConvert.SerializeObject(user, Formatting.None, serializerSettings); |
This should work:
var settings = new JsonSerializerSettings() { ContractResolver= new NullToEmptyStringResolver() };
var str = JsonConvert.SerializeObject(yourObj, settings);
public class NullToEmptyStringResolver : Newtonsoft.Json.Serialization.DefaultContractResolver
{
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return type.GetProperties()
.Select(p=>{
var jp = base.CreateProperty(p, memberSerialization);
jp.ValueProvider = new NullToEmptyStringValueProvider(p);
return jp;
}).ToList();
}
}
public class NullToEmptyStringValueProvider : IValueProvider
{
PropertyInfo _MemberInfo;
public NullToEmptyStringValueProvider(PropertyInfo memberInfo)
{
_MemberInfo = memberInfo;
}
public object GetValue(object target)
{
object result = _MemberInfo.GetValue(target);
if (_MemberInfo.PropertyType == typeof(string) && result == null) result = "";
return result;
}
public void SetValue(object target, object value)
{
_MemberInfo.SetValue(target, value);
}
}
json null的更多相关文章
- go json null字段的转换
最近试了试go中对json null字段进行转换,代码如下: struct 转 json: package main import ( "encoding/json" " ...
- 处理 JSON null 和空数组及对象
描述了对 JSON 数据中使用的 null 和空数组及对象的处理. JSON 数据具有 null 和空数组及对象的概念.此部分说明其中每个概念如何映射到 null 和未设置的数据对象概念. Null ...
- NewtonSoft.Json NULL转空字符串
from:http://www.cnblogs.com/hetuan/articles/4565702.html NewtonSoft.Json对需要转为JSON字符串的对象的NULL值以及DBNul ...
- Json Null 不输出
[TestMethod] public void NodeTest() { datanode d = new datanode() ...
- Newtonsoft.Json null值不序列化
如果对当前序列化的实体全部生效的话使用如下: var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandl ...
- Spring mvc json null
http://blog.csdn.net/zdsdiablo/article/details/9429263
- NewtonSoft对象转json时,把 NULL 转 "" , 过滤 NULL, DateTime 时间类型去除 T
IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = &quo ...
- Java版本:识别Json字符串并分隔成Map集合
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
- 原生JS:JSON对象详解
JSON对象 支持到IE8,旧版的IE需要Polyfill 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/docs/Web ...
随机推荐
- learning makefile multiple target
- erlang大法好
可惜haxe不能生成erlang.不过没关系,s6k输入法的实际执行方案,现在由typescript改用haxe.cdt3的ts地位不变. 以后这个博客大部分内容都是跟haxe/typescript相 ...
- scrapy框架初级
scrapy入门教程:https://scrapy-chs.readthedocs.io/zh_CN/0.24/intro/tutorial.html 一.安装 python模块网站,应用文件放置在s ...
- Oracle 创建存储过程 提示权限不足或者提示表和视图不存在问题
grant create view to hospital; --授予查询权限 grant select any table to hospital; --授予权限 grant select any ...
- dos脚本
关于dos命令行脚本编写 dos常用命令另查 开始之前先简单说明下cmd文件和bat文件的区别:在本质上两者没有区别,都是简单的文本编码方式,都可以用记事本创建.编辑和查看.两者所用的命令行代码也 ...
- 基于Verilog的串口发送程序
一.模块框图及基本思路 tx_bps_module:波特率时钟产生模块 tx_control_module:串口发送的核心控制模块 tx_module:前两个模块的组合 control_module: ...
- 根据ip获取地理信息
function getIPLoc_sina($queryIP){ $url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?form ...
- L2-020. 功夫传人*
L2-020. 功夫传人 参考博客 #include<vector> #include<cstring> #include<algorithm> using nam ...
- j教你如何用erlang-tuple
元组是用来保存一组数据元素的复合数据类型,其中数据元素是要求为erlang的数据类型,单不一定要是相同的类型,元组使用封闭的花括号{}来定义,里面的元素有逗号隔开. 例如: 1> {1,2,3} ...
- python int str
1. int 类型转换 a = "123" b = int(a) b = b+10 print(type(a),a) print(type(b),b) 2. int(num,bas ...