日期:2014-05-18 浏览次数:20595 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using Newtonsoft.Json;
using System.ServiceModel.Web;
namespace UtityClass
{
public class ExtJson<T>
{
public static T ReadJosn(string strValue)
{
System.IO.MemoryStream Memory = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(strValue));
DataContractJsonSerializer dataJson = new DataContractJsonSerializer(typeof(T));
return (T)dataJson.ReadObject(Memory);
}
public static string WriteJson(T strValue)
{
DataContractJsonSerializer dataJson = new DataContractJsonSerializer(typeof(T));
System.IO.MemoryStream Menory = new System.IO.MemoryStream();
dataJson.WriteObject(Menory, strValue);
byte[] jsonByte = new byte[Menory.Length];
Menory.Position = 0L;//[0] int32--[0L] int64
Menory.Read(jsonByte, 0, (int)Menory.Length);
return System.Text.Encoding.UTF8.GetString(jsonByte);
}
}
}