c#3.5关于JSON简单操作的实用帮助类(泛型实现)

ps:需要添加System.ServiceModel.Web,System.Runtime.Serialization

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Json;

namespace DotNet.Common.Util
{
/// <summary>
/// Json操作帮助类
/// </summary>
public static class JsonHelper
{
#region DataContractJsonSerializer

/// <summary>
/// 对象转换成json
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="jsonObject">需要格式化的对象</param>
/// <returns>Json字符串</returns>
public static string DataContractJsonSerialize<T>(T jsonObject)
{
DataContractJsonSerializer serializer
= new DataContractJsonSerializer(typeof(T));
string json = null;
using (MemoryStream ms = new MemoryStream()) //定义一个stream用来存发序列化之后的内容
{
serializer.WriteObject(ms, jsonObject);
json
= Encoding.UTF8.GetString(ms.GetBuffer()); //将stream读取成一个字符串形式的数据,并且返回
ms.Close();
}
return json;
}

/// <summary>
/// json字符串转换成对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="json">要转换成对象的json字符串</param>
/// <returns></returns>
public static T DataContractJsonDeserialize<T>(string json)
{
DataContractJsonSerializer serializer
= new DataContractJsonSerializer(typeof(T));
T obj
= default(T);
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
obj
= (T)serializer.ReadObject(ms);
ms.Close();
}
return obj;
}

#endregion

}
}

 

posted @ 2010-08-26 20:38  nnsword  阅读(1254)  评论(0编辑  收藏  举报