首先引用dll :System.Web.Extensions。
再自定义转换器代码如下:
public class JavaScriptSerilizeConvert : JavaScriptConverter
{
//支持的需要转换的类型,是集合可以是多个
public override IEnumerable<Type> SupportedTypes => new List<Type>(new Type[] { typeof(Entity) }); public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
//这样写,不管什么类型的对象都能反序列化了
var t = Activator.CreateInstance(type); var props = type.GetProperties(); if (dictionary != null)
{
foreach (var item in dictionary)
{
//这里有可能大小写不同,
//兼容大小写
var p = props.Where(a => a.Name.ToLower() == item.Key.ToLower()).ToList();
if(p.Count>1)
p = props.Where(a => a.Name== item.Key).ToList(); if (p.Count > 0)
{
int val = 0; if (p[0].PropertyType == typeof(int))
{
if (int.TryParse(item.Value.ToString(), out val))
{ p[0].SetValue(t, val);
}
}// else if double 这里要判断double 其他也一样
else
{
p[0].SetValue(t, item.Value);
} } }
return t;
}
return null;
} public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
//不同的类型实体可以做不同的处理
if (obj is Entity)
{
Entity t = obj as Entity; var dic= new Dictionary<string, object>();
var type = t.GetType(); var props = type.GetProperties(); foreach (var item in props)
{
var val = item.GetValue(obj); if (val != null)
{
dic.Add(item.Name, val);
}
} return dic;
}
else
{
return new Dictionary<string, object>();
}
}
}
控制台测试程序:
static void Main(string[] args)
{
JavaScriptSerializer serializer = new JavaScriptSerializer(); var entity = new Entity(); entity.Id = 1;
entity.Name = null;
entity.Des = "123";
可以注册多个自定义转换器
serializer.RegisterConverters(new JavaScriptConverter[] { new JavaScriptSerilizeConvert() });
var str = serializer.Serialize(entity); Console.WriteLine(str); //这里需要注意 一般在不用转换器的情况下 如果实体里属性Id的类型是int类型,
//这里2出就不要加双引号,不然报错
//我这里在自定义转换器上做了处理,所以能够把string类型的2转换成int型。
var str2 = "{ \"Id\":\"2\",\"Name\":null,\"Des\":\"\"}";
var en2 = serializer.Deserialize<Entity>(str2);
Console.WriteLine(en2.Id);
Console.ReadLine();
}
实体Entity:
public   class Entity
{
public int Id { get; set; }
public string Name { get; set; }
public string Des { get; set; }
}

这是简单的实体,如果有实体嵌套,也没关系,如果是都不序列化null,public override IEnumerable<Type> SupportedTypes => new List<Type>(new Type[] { typeof(Entity),typeof(SubEntity) });这里加上就行

【c#】JavaScriptSerializer 不序列化null值的更多相关文章

  1. 【私人定制jackson】定制jackson的自定义序列化(null值的处理)

    最近用springMVC做服务端的http+json的接口,出现一个不是特别容易解决的问题: 在对List类型的值进行处理时,有一部分服务是有做一些逻辑判断的,在逻辑判断不通过的时候会返回一个null ...

  2. 定制jackson的自定义序列化(null值的处理)

    http://www.cnblogs.com/lic309/p/5048631.html

  3. .net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值

    在.net mvc的controller中,方法返回JsonResult,一般我们这么写: [HttpPost] public JsonResult QueryFeature(string url, ...

  4. SpringBoot中配置不序列化返回值为null的属性

    package com.weiresearch.properties; import com.fasterxml.jackson.annotation.JsonInclude;import com.f ...

  5. JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值

    在.net mvc的controller中,方法返回JsonResult,一般我们这么写:   [HttpPost]   public JsonResult QueryFeature(string u ...

  6. 源码分析springboot自定义jackson序列化,默认null值个性化处理返回值

    最近项目要实现一种需求,对于后端返回给前端的json格式的一种规范,不允许缺少字段和字段值都为null,所以琢磨了一下如何进行将springboot的Jackson序列化自定义一下,先看看如何实现,再 ...

  7. JSON序列化自己主动过滤NULL值

    使用Newtonsoft.Json.dll 序列化为json时主动将NULL值过滤掉.详细做法: var jSetting = new JsonSerializerSettings {NullValu ...

  8. 【ITOO 2】使用ArrayList时的注意事项:去除多余的null值

    问题描述:在课表导入的时候,将数据从excel表里读出,然后将list批量插入到对应的课程表的数据表单中去,出现结果:当我们导入3条数据时,list.size()为3,但是实际上,list里面存在10 ...

  9. 使用JavaScriptSerializer进行序列化日期类型应该注意的问题

    原文:使用JavaScriptSerializer进行序列化日期类型应该注意的问题 JavaScriptSerializer在序列化DateTime时,是用刻度来表示的,具体在Json体现为:\/Da ...

  10. 使用JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength属性

    "/"应用程序中的服务器错误.使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错.字符串的长度超过了为 maxJsonLength 属性设置的值. ...

随机推荐

  1. RabbitMQ 04 直连模式-Java操作

    使用Java原生的方式使用RabbitMQ现在已经较少,但这是基础,还是有必要了解的. 引入依赖. <dependency> <groupId>com.rabbitmq< ...

  2. 华为会员开放服务(Membership Kit),助力移动应用快速建设会员生态

    会员开放服务(Membership Kit)是华为面向开发者提供的券码开放能力,开发者可以通过Membership Kit开展灵活多样的营销活动,助力开发者建设会员生态,实现用户运营与增量创收的目标. ...

  3. std::thread 二:互斥量(带超时的互斥量 timed_mutex())

    timed_mutex . try_lock_for . try_lock_until #include <iostream> #include <thread> #inclu ...

  4. 基于tapd的git commit规范

    现状 开发团队中,总是有人提交代码时的commit内容乱写一通,或者不明确不完整.当回溯代码的时候,很难通过commit内容定位历史记录,只能一条一条查看,找不到就要去问历史参与开发的其他同事,沟通成 ...

  5. 五款最优秀的java微服务框架

    微服务被广泛用于创建多功能的应用程序,通过组合每个功能部分并将它们逐层放在一个单元中.许多人可能没有意识到微服务是一组小型服务中制作单个应用程序的方法,每个服务都独立运行(进程). java微服务框架 ...

  6. 在 RedHat、 CentOS、 Fedora 和 Debian、 Ubuntu、 Linux Mint、 Xubuntu 等这些系统中安装 Teamviewer

    这篇指南介绍了怎么样在 RedHat. CentOS. Fedora 和 Debian. Ubuntu. Linux Mint. Xubuntu 等这些系统中安装 Teamviewer 9.Teamv ...

  7. 重新整理.net core 计1400篇[一] (.net core 命令行)

    前言 把.net core 从新整理一遍. 下面介绍命令行. 正文 运行一下:dotnet new --list 那么这个时候会返回非常多的模板给你们. 这时候会给我们列出:project Templ ...

  8. # AssertionError: The `num_classes` (3) in Shared2FCBBoxHead of MMDataParallel does not matches the length of `CLASSES` 80) in CocoDataset

    我看很多人都遇到了这个问题,有很多解决了的.我就把这篇博文再完善一下,让大家对mmdetection使用得心应手. mmdetection训练自己的数据集时报错 ️ : # AssertionErro ...

  9. marquee实现滚动

    marquee的基本语法:<marquee> ... </marquee> 参数:1.滚动方向 (direction):left(左).right(右).up(上).down( ...

  10. 说说对 Node 中的 Buffer 的理解?应用场景?

    一.是什么 在Node应用中,需要处理网络协议.操作数据库.处理图片.接收上传文件等,在网络流和文件的操作中,要处理大量二进制数据,而Buffer就是在内存中开辟一片区域(初次初始化为8KB),用来存 ...