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();
}
}
C#序列化JSON
时间: 2024-10-10 05:10:03