using System;
using System.Data;
using System.Web;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;
public partial class project_manage_TJ : CommonPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
public string ObjectToJson(object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
MemoryStream stream = new MemoryStream();
serializer.WriteObject(stream, obj);
byte[] dataBytes = new byte[stream.Length];
stream.Position = 0;
stream.Read(dataBytes, 0, (int)stream.Length);
return Encoding.UTF8.GetString(dataBytes);
}
}对象类型转json类型
最新推荐文章于 2024-08-04 03:46:33 发布
本文介绍了一种使用C#将对象转换为JSON字符串的方法。通过DataContractJsonSerializer类实现序列化过程,文中提供了完整的代码示例,展示了如何将任意.NET对象序列化为UTF-8编码的JSON字符串。
279

被折叠的 条评论
为什么被折叠?



