组件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 ...
随机推荐
- 如何在 IIS 上搭建 mercurial server
mercurial server 对于代码管理工具,更多的人可能更熟悉 Git 一些(Git 太火了).其实另外一款分布式代码管理工具也被广泛的使用,它就是 mercurial.多人协作时,最好能够通 ...
- [常见问题]在Linux下执行Redis命令不起作用.
redis 127.0.0.1:6379> 这个后面无论输入什么命令都没有返回 ok 或者其他的信息,一直保持截图的状态: 解决方法:在SecureCRT中设置Options-->Sess ...
- JS操作页面
DOM操作 1 windows对象操作 属性(值或者子对象): opener:打开当前窗口的源窗口,如果当前窗口是首次启动浏览器打开的,则opener是null.可以利用这属性来关闭源窗口 方法(函数 ...
- sdk更新代理设置
sdk更新代理设置 http://www.cnblogs.com/zhoujg/p/4560998.html
- Python内建的对象列表
Python内建的对象列表 刚写Python肯定会遇到这样的情况,想写些什么,但又不知从何写起... 在我看来问题在于我们不知道有什么东东可以拿来玩,这里列出Python的内建对象,稍微归类了一下,多 ...
- OpenCascade HLR for Pipe Model
OpenCascade HLR for Pipe Model eryar@163.com 摘要Abstract:在工厂辅助设计(Plant Design)或船舶辅助设计(Ship Design)等CA ...
- ASP.NET MVC 4.0中选择Windows 验证默认出错拒绝访问的原因和解决方案
在VS 2012或者2013 中,根据模板创建一个ASP.NET MVC 4.0的应用程序,选择下面的模板 然后选择Intranet Application 不对源代码做任何修改,直接按下F5调试,会 ...
- Cocos2d-x 3.2 学习笔记(十一)Game 2048
一.规则 游戏2048的规则很简单 2+2=4 4+4=8 8+8=16 16+16=32 ……1024+1024=2048 这游戏挺火的……不过做起来也不难,就用cocos2dx做一下,首先我也没 ...
- 数据可视化(8)--D3数据的更新及动画
最近项目组加班比较严重,D3的博客就一拖再拖,今天终于不用加班了,赶紧抽点时间写完~~ 今天就将D3数据的更新及动画写一写~~ 接着之前的博客写~~ 之前写了一个散点图的例子,下面可以自己写一个柱状图 ...
- 从重置input file标签中看jQuery的 .val() 和 .attr(“value”) 区别
背景: 在清空input file标签选中值时,分别用了以下方法,发现有的对有的错: [√]$("#file")[0].value = ""; [√]$(&qu ...