C#生成JSON数据(序列化对象)
C#处理JSON数据的时候需要引用Newtonsoft.Json.DLL文件,这个DLL文件可以从其官方网站 https://www.newtonsoft.com/json 下载到本地,然后引入到项目里面,在需要处理的文件里引用即可。
using Newtonsoft.Json;
生成json对象
Boolean status = true;
//创建对象
ActivityModel model = new ActivityModel
{
Status = status,
Message = "成功",
};
//对象序列化Json
string strJson = JsonConvert.SerializeObject(model);
context.Response.ContentType = "text/plain";
context.Response.Write(strJson);
model.cs
public class ActivityModel
{
public bool Status { get; set; }
public string Message { get; set; }
}