public static byte[] writeValueAsZipByte(List<CraneDataDtls> dtls) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
GZIPOutputStream outputStream = null;
try {
outputStream = new GZIPOutputStream(byteOutputStream);
mapper.writeValue(outputStream, dtls);
} catch (JsonGenerationException e) {
LOG.error("Error when convert Pojo to Json ", e);
throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
} catch (JsonMappingException e) {
LOG.error("Error when convert Pojo to Json ", e);
throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
} catch (IOException e) {
LOG.error("Error when convert Pojo to Json ", e);
throw new UnhandledException("Server Error, please try again, or contact supportor\r\n");
}
byte[] arr = byteOutputStream.toByteArray();
return arr;
}

Java 上述代码:

JSON 数据转换 :

代码:

//{
// "Name": "Apple",
// "Expiry": "2008-12-28T00:00:00",
// "Sizes": [
// "Small"
// ]
//}
static void Main(string[] args)
{
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(, , );
product.Sizes = new string[] { "Small" ,"Large"};
string json = JsonConvert.SerializeObject(product);
Debug.WriteLine(json);
Console.ReadKey(); Product productClone = JsonConvert.DeserializeObject<Product>(json);
Debug.WriteLine(productClone.Name);
Debug.WriteLine(productClone.Expiry);
for (int i = ; i < productClone.Sizes.Length; i++)
{
Debug.Write(productClone.Sizes[i] + " ");
}
Console.ReadKey(); }

https://json.codeplex.com/downloads/get/832296

http://www.cnblogs.com/piaopiao7891/p/3624779.html

    public class Customer
{
public string CustomerName;
public int Unid;
}
        public void CallCustomerToJson()
{
Customer cc = new Customer { Unid = , CustomerName = "John" };
string strJson = CustomerToJsonSerialize(cc); Console.WriteLine(strJson);
} public void CallJsonToCustomer()
{
string strJson = "{\"CustomerName\":\"John\",\"Unid\":1}";
Customer c = JsonToCustomerDeserialize(strJson) as Customer; Console.WriteLine(c.Unid + " " + c.CustomerName);
} public void GenericCallCustomerToJson()
{
Customer cc = new Customer { Unid = , CustomerName = "John" };
string strJsons = GenericToJson<Customer>(cc);
Console.WriteLine(strJsons);
} public void GenericCallJsonToCustomer()
{
string strJson = "{\"CustomerName\":\"John\",\"Unid\":1}";
Customer c = GenericFromJson<Customer>(strJson);
Console.WriteLine(c.Unid + " " + c.CustomerName);
} #region Json /// <summary>
/// Json Serialize
/// </summary>
/// <param name="customer"></param>
/// <returns></returns>
public string CustomerToJsonSerialize(Customer customer)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Customer)); MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, customer); string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close(); return strReturn;
} /// <summary>
/// Json DeSerialize
/// </summary>
/// <param name="strJson"></param>
/// <returns></returns>
public object JsonToCustomerDeserialize(string strJson)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(Customer));
MemoryStream ms = new MemoryStream(Encoding.Default.GetBytes(strJson)); return ds.ReadObject(ms);
} /// <summary>
/// GenericToJson
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="t"></param>
/// <returns></returns>
public static string GenericToJson<T>(T t)
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream();
ds.WriteObject(ms, t); string strReturn = Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return strReturn;
} /// <summary>
/// GenericFromJson
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strJson"></param>
/// <returns></returns>
public static T GenericFromJson<T>(string strJson) where T : class
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(strJson)); return ds.ReadObject(ms) as T;
} #endregion
    [DataContract]//必须申明,否则无法序列化
public class Person
{
[DataMember(Name = "PersonName")]//必须申明,否则无法序列化
private string Name; [DataMember(Name = "PersonSex")]//必须申明,否则无法序列化
private bool Sex; public Person(string name, bool sex)
{
this.Name = name;
this.Sex = sex;
} public override string ToString()
{
return "姓名:" + this.Name + "\t性别:" + (this.Sex ? "男" : "女");
} } [DataContract]
public class Programmer : Person
{
[DataMember(Name = "ProgramLanguage")]
private List<string> Languages; public Programmer(string name, bool sex, List<string> languages)
: base(name,sex)
{
this.Languages = languages;
} public override string ToString()
{
StringBuilder str = new StringBuilder();
str.Append(base.ToString() + "\t编程语言:");
foreach (string lang in this.Languages)
{
str.Append(lang + " ");
} return str.ToString();
} } public void DataContractSampleCode()
{
string filePath = @"D:\ObjectJson.txt"; List<string> languages = null;
languages = new List<string>(new string[] { "C#", "Java" });
languages = new List<string>(new string[] { "C#", "C++" });
languages = new List<string>(new string[] { "C#", "C++", "C", "Java" }); List<Programmer> list = new List<Programmer>();
list.Add(new Programmer("Coder1", true, languages));
list.Add(new Programmer("Coder2", false, languages));
list.Add(new Programmer("Coder3", true, languages)); //序列化对象
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List<Programmer>)); MemoryStream ms = new MemoryStream();
ser.WriteObject(ms, list);//将对象转换成json存储
string jsonStr = Encoding.Default.GetString(ms.ToArray());
File.WriteAllText(filePath, jsonStr);
ms.Position = ;
ms.Dispose();//关闭内存流 //反序列化对象
list.Clear(); using (FileStream fStream = File.Open(filePath,FileMode.OpenOrCreate))
{
list = (List<Programmer>)ser.ReadObject(fStream);
foreach (Programmer p in list)
{
Console.WriteLine(p);
}
} }

http://www.cnblogs.com/junbird-nest/archive/2012/03/23/2413441.html

http://www.cnblogs.com/phoenixtrees/archive/2011/05/01/2033847.html

SingletonBaseTemplate的更多相关文章

随机推荐

  1. java File类的基本使用

    package com.soar.file; import java.io.File; import java.io.IOException; public class Demo2_FileMetho ...

  2. D3D 点列练习

    画四个点在窗体. #pragma once #pragma comment(lib,"d3d9.lib") #pragma comment(lib,"d3dx9.lib& ...

  3. [Grid Layout] Describe a grid layout using grid-template-areas

    We can describe the nature of a grid in an ‘ASCII-art’ way with grid-template-areas. Let’s see how t ...

  4. 矩阵分解(matrix factorization)

    1. 基本概念 针对高维空间中的数据集,矩阵分解通过寻找到一组基及每一个数据点在该基向量下的表示,可对原始高维空间中的数据集进行压缩表示. 令 X=[x1,⋯,xm]∈Rm×n 为数据矩阵,矩阵分解的 ...

  5. 5.7-基于Binlog+Position的复制搭建

    基本环境   Master Slave MySQL版本 MySQL-5.7.16-X86_64 MySQL-5.7.16-X86_64 IP 192.168.56.156 192.168.56.157 ...

  6. Android 开发者工具

    30多个Android 开发者工具 文中部分工具是收费的,但是绝大多数都是免费的. FlowUp 这是一个帮助你跟踪app整体性能的工具,深入分析关键的性能数据如FPS, 内存, CPU, 磁盘, 等 ...

  7. 【BZOJ 1016】 [JSOI2008]最小生成树计数(matrix-tree定理做法)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1016 [题意] [题解] /* 接上一篇文章; 这里用matrix-tree定理搞最小 ...

  8. Tomcat系列之服务器的安装与配置以及各组件详解

    Tomcat系列之服务器的安装与配置以及各组件详解 大纲 一.前言 二.安装与配置Tomcat 三.Tomcat 目录的结构 四.Tomcat 配置文件 注,本文的测试的操作系统为CentOS 6.4 ...

  9. Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64

    Compile Graphics Magick, Boost, Botan and QT with MinGW64 under Windows 7 64 Sun, 01/01/2012 - 15:43 ...

  10. XSS攻击之基础篇:HTML标签与字符串的渲染

    <body> XSS攻击之基础篇:HTML标签与字符串的渲染 测试 <div class="a1"> </div> <div class= ...