组件Newtonsoft.Json实现object2json转换
很多情况下,我们需要把数据类型做一些转换,供其它外部的子系统调用。
最为典型的是生成json格式供javascript作调用。
现成的组件Newtonsoft.Json可以实现object2json之间的转换。
Newtonsoft.Json.JavaScriptConvert.SerializeObject(object)可以执行json的序列化,也是反序列化的方法。
常见的场景:
A系统提供用户资料(MemberInfo)给子系统B调用,但用户资料中有些内容是不能公开的,如Email地址。
本文由网页教学网(www.webjx.com)发布!转载和采集的话请不要去掉!谢谢。
直接用Newtonsoft.Json.JavaScriptConvert.SerializeObject好像是不行的,它会把object中的所有属性列出。
我的做法是这样的:
定义一个特性类,该特性类只允许用于类的属性上
view plaincopy to clipboardprint?
[AttributeUsage(AttributeTargets.Property)]
public class DenyReflectionAttrubite : Attribute
{
public DenyReflectionAttrubite() { }
}
[AttributeUsage(AttributeTargets.Property)]
public class DenyReflectionAttrubite : Attribute
{
public DenyReflectionAttrubite() { }
}
MemberInfo类
view plaincopy to clipboardprint?
public class MemberInfo
{
public int Uid
{
get;
set;
}
public string UserName
{
get;
set;
}
public string NickName
{
get;
set;
}
[DenyReflectionAttrubite]
public string Password
{
get;
set;
}
[DenyReflectionAttrubite]
public string Email
{
get;
set;
}
//.......................
}
public class MemberInfo
{
public int Uid
{
get;
set;
}
public string UserName
{
get;
set;
}
public string NickName
{
get;
set;
}
[DenyReflectionAttrubite]
public string Password
{
get;
set;
}
[DenyReflectionAttrubite]
public string Email
{
get;
set;
}
//.......................
}
至于DenyReflectionAttrubite特性如何使用,下面就会提出。
json生成
view plaincopy to clipboardprint?
public void Builder()
{
using (jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.None;
jsonWriter.Indentation = 4;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("member");
jsonWriter.WriteStartArray();
jsonWriter.WriteStartObject();
Type settingsType = this.member.GetType();
foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
{
try
{
//在这里对DenyReflectionAttrubite特性的属性跳过处理
if (propertyInformation.GetCustomAttributes(typeof(DenyReflectionAttrubite), false).Length > 0) continue;
object propertyValue = propertyInformation.GetValue(member, null);
string valueAsString = propertyValue.ToString();
if (propertyValue.Equals(null))
{
valueAsString = String.Empty;
}
if (propertyValue.Equals(Int32.MinValue))
{
valueAsString = String.Empty;
}
if (propertyValue.Equals(Single.MinValue))
{
valueAsString = String.Empty;
}
jsonWriter.WritePropertyName(propertyInformation.Name.ToLower());
jsonWriter.WriteValue(valueAsString.Trim());
}
catch { }
}
jsonWriter.WriteEndObject();
jsonWriter.WriteEnd();
jsonWriter.WriteEndObject();
}
}
public void Builder()
{
using (jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.None;
jsonWriter.Indentation = 4;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("member");
jsonWriter.WriteStartArray();
jsonWriter.WriteStartObject();
Type settingsType = this.member.GetType();
foreach (PropertyInfo propertyInformation in settingsType.GetProperties())
{
try
{
//在这里对DenyReflectionAttrubite特性的属性跳过处理
if (propertyInformation.GetCustomAttributes(typeof(DenyReflectionAttrubite), false).Length > 0) continue;
object propertyValue = propertyInformation.GetValue(member, null);
string valueAsString = propertyValue.ToString();
if (propertyValue.Equals(null))
{
valueAsString = String.Empty;
}
if (propertyValue.Equals(Int32.MinValue))
{
valueAsString = String.Empty;
}
if (propertyValue.Equals(Single.MinValue))
{
valueAsString = String.Empty;
}
jsonWriter.WritePropertyName(propertyInformation.Name.ToLower());
jsonWriter.WriteValue(valueAsString.Trim());
}
catch { }
}
jsonWriter.WriteEndObject();
jsonWriter.WriteEnd();
jsonWriter.WriteEndObject();
}
}
组件Newtonsoft.Json实现object2json转换的更多相关文章
- Newtonsoft.Json 把对象转换成json字符串
var resultJson = new { records = rowCount, page = pageindex, //总页数=(总页数+页大小-1)/页大小 total = (rowCount ...
- C#将集合和Json格式互相转换的几种方式
1.使用微软自带的System.Web.Extensions.dll转换,该DLL文件一般存在于如下路径:c:\Program Files\Reference Assemblies\Microsoft ...
- Newtonsoft.Json日期转换
在使用EasyUI做后台时,使用表格datagrid,用Newtonsoft.Json转换为Json格式后,时间显示为2013-06-15 T00:00:00形式. 后来研究了一下Newtonsoft ...
- Newtonsoft.Json转换强类型DataTable错误:Self referencing loop detected with type ......
问题,在使用Newtonsoft.Json对强类型的DataTable进行系列化时会出现循环引用错误 解决办法,不要直接系列化强类型的DataTable,改为 JsonConvert.Serializ ...
- Newtonsoft.Json 转换DateTime类型为字符串时,串内部会有一个T。解决方案
使用Newtonsoft.Json 转换DateTime类型时,若使用标准转换,则字符串内会有一个T(虽然再转换成DateTime没有问题). 若要转换成DateTime没有T,可以加上特性: pub ...
- Asp.Net中使用Newtonsoft.Json转换,读取,写入
using Newtonsoft.Json;using Newtonsoft.Json.Converters; //把Json字符串反序列化为对象目标对象 = JsonConvert.Deserial ...
- Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty
原文:Net Core 下 Newtonsoft.Json 转换字符串 null 替换成string.Empty public class NullToEmptyStringResolver : De ...
- 【转载】 C#使用Newtonsoft.Json组件来反序列化字符串为对象
在Asp.Net网站开发的过程中,很多时候会遇到对象的序列化和反序列化操作,Newtonsoft.Json组件是专门用来序列化和反序列化操作的一个功能组件,引入这个DLL组件后,就可使用JsonCon ...
- 【转载】C#使用Newtonsoft.Json组件来序列化对象
在Asp.Net网站开发的过程中,很多时候会遇到对象的序列化和反序列化操作,Newtonsoft.Json组件是专门用来序列化和反序列化操作的一个功能组件,引入这个DLL组件后,就可使用JsonCon ...
随机推荐
- django开发个人简易Blog——构建项目结构
开发之前第一步,就是构造整个的项目结构.这就好比作一幅画,第一步就是描绘轮廓,有了轮廓,剩下的就是慢慢的填充细节.项目结构规划如下图: 项目结构描述: 本项目以fengzhengBlog为根目录. a ...
- C++虚函数表
大家知道虚函数是通过一张虚函数表来实现的.在这个表中,主要是一个类的虚函数的地址表,这张表解决了继承.覆盖的问题,其内容真是反应实际的函数.这样,在有虚函数的类的实例中,这个表分配在了这个实例的内存中 ...
- Java中 VO、 PO、DO、DTO、 BO、 QO、DAO、POJO的概念
PO(persistant object) 持久对象 在 o/r 映射的时候出现的概念,如果没有 o/r 映射,没有这个概念存在了.通常对应数据模型 ( 数据库 ), 本身还有部分业务逻辑的处理.可以 ...
- 1.安装Redis
首要条件:安装VMware,在虚拟机中安装CentOS. 安装步骤: 1.打开终端(Terminal) 2.在终端输入:wget http://download.redis.io/releases/r ...
- opcode的执行
原文链接:http://www.orlion.ga/1001/ 当.php文件被编译为opcode后,下一步的执行并非是把opcode编译为机器码而是类似于如下的方式执行: while (TRUE) ...
- struts2标签
一.通用标签 1.property Name Required Default Evaluated Type Description default false false String ...
- javase基础复习攻略《二》
今天就开始的真正走进JAVASE的世界,本篇介绍的是:JAVASE基础语法,大家如果有C语言的基础,对于本节内容一定感觉非常轻松,编程语言之间的都是相通的,只不过C语言属于面向过程编程,而JAVA语言 ...
- Uvaoj 10048 - Audiophobia(Floyd算法变形)
1 /* 题目大意: 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! */ #include< ...
- 10034 - Freckles 克鲁斯克尔最小生成树!~
/* 10034 - Freckles 克鲁斯克尔最小生成树!- */ #include<iostream> #include<cstdio> #include<cmat ...
- 《BI那点儿事》数据挖掘的主要方法
一.回归分析目的:设法找出变量间的依存(数量)关系, 用函数关系式表达出来.所谓回归分析法,是在掌握大量观察数据的基础上,利用数理统计方法建立因变量与自变量之间的回归关系函数表达式(称回归方程式).回 ...