.net fw自带json序列化

引用C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v5.0\System.ServiceModel.Web.dll

using System.Runtime.Serialization.Json;

internal string SerializeToJsonString(object objectToSerialize)
{
    using (MemoryStream ms = new MemoryStream())
    {
        DataContractJsonSerializer serializer =
                new DataContractJsonSerializer(objectToSerialize.GetType());
        serializer.WriteObject(ms, objectToSerialize);
        ms.Position = 0;

        using (StreamReader reader = new StreamReader(ms))
        {
            return reader.ReadToEnd();
        }
    }
}

internal T DeSerializeJsonString<T>(string data)
{
    T ot;
    using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(data)))
    {
        DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
        ot = (T)serializer.ReadObject(ms);
    }
    return ot;
}

你可能感兴趣的:(.net)