Framework版本:.Net Framework 4

使用DataContractJsonSerializer时,实体请使用注解,格式如下

1、实体使用注解,并且提供get和set的public访问器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
namespace ReligionServer.vo
{
[DataContract]
public class Inspection : Model.Inspection, IComparable<vo.Inspection>
{ [DataMember(Order = 0)]//Order表示输出顺序
public new string Time { get; set; } [IgnoreDataMember]//表示不参与Json转换
public String Msg { get; set; } public int Order { get; set; } [DataMember]
public String Period { get; set; } int IComparable<Inspection>.CompareTo(Inspection other)
{
int result;
//正序
if (this.RTime > other.RTime) {
result = ;
} else if (this.RTime < other.RTime) {
result = -;
}
else {
result = ;
}
//反序
if (this.Order == -)
{
if (result < ) {
result = System.Math.Abs(result);
} else if (result > ) {
result = - result;
}
} //我好蠢,这里做了排序,又对Service中对noCheckList做了分页(正序和反序分页) return result;
}
}
}

2、JSON工具源码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ReligionServer.util;
using System.Text;
using System.Runtime.Serialization.Json; namespace ReligionServer.util {
public class JsonUtil <T>{ private static DataContractJsonSerializer jsonSerializer;
private static T t; static JsonUtil(){
t = (T)ReflectionUtil.Instance(new JsonUtil<T>().GetType());
Console.WriteLine(t.GetType());
jsonSerializer = new DataContractJsonSerializer(typeof(T));
} public static String ObjToJson(T t){
using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
//System.Diagnostics.Debug.WriteLine(t);
jsonSerializer.WriteObject(ms, t);
//System.Diagnostics.Debug.WriteLine(ms.ToArray().Length);
StringBuilder builder = new StringBuilder();
builder.Append(Encoding.UTF8.GetString(ms.ToArray()));
//System.Diagnostics.Debug.WriteLine(builder.Length);
//System.Diagnostics.Debug.WriteLine(builder.ToString());
return builder.ToString();
}
}
}
}

C#——JSON操作类简单封装(DataContractJsonSerializer)的更多相关文章

  1. C#——图片操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  2. C#——文件操作类简单封装

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...

  3. C#封装的一个JSON操作类

    using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...

  4. ASP.NET2.0 Newtonsoft.Json 操作类分享

    JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...

  5. MySQL操作类的封装(PHP)

    <?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...

  6. 公共的Json操作类

    using System; using System.Data; using System.Text; using System.Collections.Generic; using System.R ...

  7. ADO.NET操作PostgreSQL:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

  8. ADO.NET操作SQL Server:数据库操作类(已封装)

    1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...

  9. Java实体与Json操作类

    import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.Jav ...

随机推荐

  1. 安全日志:/var/log/secure

    /var/log/secure 一般用来记录安全相关的信息,记录最多的是哪些用户登录服务器的相关日志,如果该文件很大,说明有人在破解你的 root 密码 [root@localhost ~]$ tai ...

  2. SQL基础--视图

     视图其实就是一条查询SQL语句,用于显示一个或多个表或其它视图中相关数据. 创建视图: CREATE [OR REPLACE] [FORCE |NOFORCE ]VIEW view_name [al ...

  3. 关于MCU的烧录,下载与其他接口的比较(二)

    单片机应用系统由硬件和软件组成,软件的载体是硬件的程序存储器,程序存储器采用只读存储器,这种存储器在电源关闭后,仍能保存程序,在系统上电后,CPU可取出这些指令重新执行.只读存储器(Read Only ...

  4. ubuntu的安装方法

    Ubuntu 是一个启动速度超快.界面友好.安全性好的开源操作系统,它由全球顶尖开源软件专家开发,适用于桌面电脑.笔记本电脑.服务器以及上网本等,并且它可以永久免费使用.如果你厌倦了Windows,如 ...

  5. 学习JQuery - 10

    第四章 Styling and Animating 1. 使用内联属性修改CSS 我们知道HTML在onload时会读取css的各项值. 那么,我们能不能在之后的操作中改变css值呢? 答案是肯定的! ...

  6. 百度地图API功能

    984aca5bc78b070e59f34e230f11cf6d http://api.map.baidu.com/api?v=2.0&ak=您的密钥" <html> & ...

  7. ios UIImage图片拉伸 resizableImageWithCapInsets:

    常见的按钮添加和背景设置如下: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 130, 160, 44)];[bu ...

  8. windowSoftInputMode

    有个问题困扰我一晚上,每次进入Activity后,EditText自动获得焦点弹出软键盘,键盘遮挡listView,使得无法显示最后一条消息.我在edittext点击事件中也设定了,listView. ...

  9. mysql max_allowed_packet参数值改大后,莫名被还原

    mysql数据库用innodb引擎,mysql max_allowed_packet在my.cnf中值加大后,够一段时间,系统会莫名把这个参数的值改小. innodb_buffer_pool_size ...

  10. 310实验室(六)CMake学习心得

    树形结构方式布局. OTL 中每一个文件中的CMakeLists.txt 有不同的作用:按查看文件的先后顺便进行分层理解, 根文件即第一次 中的.txt是 启用 CMAKE_MODULE_PATH模板 ...