首页 > 代码库 > C#序列化JSON

C#序列化JSON

 public static string ConvertToJsonString<T>(T instance)       

  {             using (MemoryStream stream = new MemoryStream())            

        {

      DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));

                serializer.WriteObject(stream, instance);

                StringBuilder sb = new StringBuilder();

                sb.Append(Encoding.UTF8.GetString(stream.ToArray(), 0, stream.ToArray().Length));

                return sb.ToString();            

             }        

}