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 ...
随机推荐
- 代码统计工具-cloc
官网地址:http://cloc.sourceforge.net/ https://sourceforge.NET/projects/cloc/files/ 下载得到cloc-1.64.exe Clo ...
- 【golang】kafka
这篇博客是用来记录用go安装及操作kafka库的时候踩到的坑~ 安装kafka库 首先我参考了博客:https://blog.csdn.net/tflasd1157/article/details/8 ...
- 版本控制git第一篇
一.git的下载与安装 参考:https://blog.51cto.com/wangfeng7399/2352524 Git 是一个开源的分布式版本控制软件,用以有效.高速的处理从很小到非常大的项目版 ...
- RAID的基本介绍
一.传统磁盘的劣势 影响计算机性能的组件一般包括:CPU.主板总线IO.内存IO.硬盘IO.网卡IO.现代处理器性能已经很高了,但是计算机整体IO性能较弱,严重影响了计算机性能 现代的计算机总线.内存 ...
- np.newaxis
http://blog.csdn.net/mameng1/article/details/54599306
- locate语法
1.命令格式:locate [参数] [文件] 2.命令功能:locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期性建立的 ...
- L2-013. 红色警报(并查集)*
L2-013. 红色警报 参考博客 #include <cstdio> #include <algorithm> #include <iostream> #incl ...
- MySQL - 常见的存储引擎
数据库存储引擎是数据库底层软件组织,数据库管理系统(DBMS)使用数据引擎进行创建.查询.更新和删除数据,不同的存储引擎... 存储引擎 数据库存储引擎: 是数据库底层软件组织,数据库管理系统(DBM ...
- Linux学习man page
1.3.8需要记住,分别代表用户操作.函数库和管理指令. ##man -f command 显示出命令的所有说明文档.等同于 whatis ##man -k key 显示出带key的所有说明文档.等同 ...
- day03运算符 逻辑运算符
今日内容 运算符 算术运算符 取模% 打印1~100基数 #模2余1的为基数 #以1 3 5 7 9结尾的为奇数 # count =1 # while count<100: # print(co ...