{
"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);
26down voteaccepted

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的更多相关文章

  1. go json null字段的转换

    最近试了试go中对json null字段进行转换,代码如下: struct 转 json: package main import ( "encoding/json" " ...

  2. 处理 JSON null 和空数组及对象

    描述了对 JSON 数据中使用的 null 和空数组及对象的处理. JSON 数据具有 null 和空数组及对象的概念.此部分说明其中每个概念如何映射到 null 和未设置的数据对象概念. Null ...

  3. NewtonSoft.Json NULL转空字符串

    from:http://www.cnblogs.com/hetuan/articles/4565702.html NewtonSoft.Json对需要转为JSON字符串的对象的NULL值以及DBNul ...

  4. Json Null 不输出

    [TestMethod]        public void NodeTest()        {            datanode d = new datanode()           ...

  5. Newtonsoft.Json null值不序列化

    如果对当前序列化的实体全部生效的话使用如下: var jSetting = new JsonSerializerSettings {NullValueHandling = NullValueHandl ...

  6. Spring mvc json null

    http://blog.csdn.net/zdsdiablo/article/details/9429263

  7. NewtonSoft对象转json时,把 NULL 转 "" , 过滤 NULL, DateTime 时间类型去除 T

    IsoDateTimeConverter timeConverter = new IsoDateTimeConverter(); timeConverter.DateTimeFormat = &quo ...

  8. Java版本:识别Json字符串并分隔成Map集合

    前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...

  9. 原生JS:JSON对象详解

    JSON对象 支持到IE8,旧版的IE需要Polyfill 本文参考MDN做的详细整理,方便大家参考[MDN](https://developer.mozilla.org/zh-CN/docs/Web ...

随机推荐

  1. doxygen

    //commndline: doxygen Doxyfile /**comment /* /** time diff@pre precondition@post endcondition@throw ...

  2. 为Linux技术学习推荐看的书籍—《Linux就该这么学》

    成长,是一种经历:经历,是一种人生的体验.人生的意义不在于我们拥有了什么,而在于从中我们体悟了什么.在这短短的三年,却在我的人生中弥足珍贵,在脑海中记忆犹新,在这大学三年里,我从一个莽撞少年成长为一名 ...

  3. Backpack VI

    Given an integer array nums with all positive numbers and no duplicates, find the number of possible ...

  4. python mysql and ORM

    http://www.cnblogs.com/alex3714/articles/5950372.html 9. ORM sqlachemy学习 http://www.cnblogs.com/alex ...

  5. Linux应用调试 :使用gdb和gdbserver进行远程调试

    一.引言 在日常程序开发中不免遇到类似空指针操作导致程序崩溃的问题,所以需要一定的手段去定位bug,而断点调试是普遍使用的技巧,比如Windows中用VC++的debug模式进单步运行.断点调试等,而 ...

  6. 1095 解码PAT准考证

    PAT 准考证号由 4 部分组成: 第 1 位是级别,即 T 代表顶级:A 代表甲级:B 代表乙级: 第 2~4 位是考场编号,范围从 101 到 999: 第 5~10 位是考试日期,格式为年.月. ...

  7. THML

    结构<!DOCTYE html> <html> <head> <meta   charset="UTF-8> <titie>< ...

  8. VBA消息框

    MsgBox函数显示一个消息框,并等待用户点击一个按钮,然后根据用户点击该按钮的动作执行. 语法 MsgBox(prompt[,buttons][,title][,helpfile,context]) ...

  9. python爬虫解析页面数据的三种方式

    re模块 re.S表示匹配单行 re.M表示匹配多行 使用re模块提取图片url,下载所有糗事百科中的图片 普通版 import requests import re import os if not ...

  10. [LeetCode&Python] Problem 458. Poor Pigs

    There are 1000 buckets, one and only one of them contains poison, the rest are filled with water. Th ...