C#——JSON操作类简单封装(DataContractJsonSerializer)
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)的更多相关文章
- C#——图片操作类简单封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...
- C#——文件操作类简单封装
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...
- C#封装的一个JSON操作类
using System; using System.Collections.Generic; using System.Collections; using System.Text; using S ...
- ASP.NET2.0 Newtonsoft.Json 操作类分享
JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...
- MySQL操作类的封装(PHP)
<?php class mysql{ /** * 报错函数 * * @param string $error */ function err($error){ die("对不起,您的操 ...
- 公共的Json操作类
using System; using System.Data; using System.Text; using System.Collections.Generic; using System.R ...
- ADO.NET操作PostgreSQL:数据库操作类(已封装)
1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...
- ADO.NET操作SQL Server:数据库操作类(已封装)
1.增.删.改通用方法 /// <summary> /// 增.删.改通用方法 /// </summary> /// <param name="commandT ...
- Java实体与Json操作类
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.Jav ...
随机推荐
- NUC970设备驱动
安装完WinUSB4NuVCOM_NUC970.exe后 USB0要配置成DEVICE 才可以在设备管理器中显示.
- C++异常 返回错误码
一种比异常终止更灵活的方法是,使用函数的返回值来指出问题.例如,ostream类的get(void)成员ASCII码,但到达文件尾时,将返回特殊值EOF.对hmean()来说,这种方法不管用.任何树脂 ...
- ubuntu安装TexturePicker
TexturePacker网页:https://www.codeandweb.com/texturepackerTexturePacker下载页面:https://www.codeandweb.com ...
- Mybatis头文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-/ ...
- iOS设计模式之类族(class cluster)
类族模式在UIKit(user interface framework)使用的范围已经远远超过我们的想象,比如,UIButton,NSArray,NSString,NSNumber等, 例如NSNum ...
- 学习JQuery - 10
第四章 Styling and Animating 1. 使用内联属性修改CSS 我们知道HTML在onload时会读取css的各项值. 那么,我们能不能在之后的操作中改变css值呢? 答案是肯定的! ...
- java基础---->java中国际化的实现
应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产.开发这样的程序的过程,就称为国际化.今天,我们就开始学习java中国际化的代码实现. Java国际化主要通过如下3个类 ...
- HTTP/2笔记之流和多路复用
零.前言 本部分将讲解HTTP/2协议中对流的定义和使用,其实就是在说HTTP/2是若何做到多路复用的. 一.流和多路复用的关系 1. 流的概念 流(Stream),服务器和客户端在HTTP/2连接内 ...
- 【Java nio】buffer
package com.slp.nio; import org.junit.Test; import java.nio.ByteBuffer; /** * Created by sanglp on 2 ...
- CSS- ie6,ie7,ie8 兼容性写法,CSS hack写法
css ie6,ie7,ie8 兼容性写法,CSS hack写法 margin-bottom:40px; /*ff的属性*/margin-bottom:140px\9; /* IE6 ...