C#将对象序列化成JSON字符串
生活随笔
收集整理的這篇文章主要介紹了
C#将对象序列化成JSON字符串
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
using System.Web.Script.Serialization;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
List<Product> products = new List<Product>(){
new Product(){Name="蘋果",Price=5.5},
new Product(){Name="橘子",Price=2.5},
new Product(){Name="干柿子",Price=16.00}
};
ProductList productlist = new ProductList();
productlist.GetProducts = products;
context.Response.Write(new JavaScriptSerializer().Serialize(productlist));
}
public bool IsReusable
{
get
{
return false;
}
}
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
}
public class ProductList
{
public List<Product> GetProducts { get; set; }
}
生成的JSON結果如下:
{"GetProducts":[{"Name":"蘋果","Price":5.5},{"Name":"橘子","Price":2.5},{"Name":"柿子","Price":16}]}
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
List<Product> products = new List<Product>(){
new Product(){Name="蘋果",Price=5.5},
new Product(){Name="橘子",Price=2.5},
new Product(){Name="干柿子",Price=16.00}
};
ProductList productlist = new ProductList();
productlist.GetProducts = products;
context.Response.Write(new JavaScriptSerializer().Serialize(productlist));
}
public bool IsReusable
{
get
{
return false;
}
}
public class Product
{
public string Name { get; set; }
public double Price { get; set; }
}
public class ProductList
{
public List<Product> GetProducts { get; set; }
}
生成的JSON結果如下:
{"GetProducts":[{"Name":"蘋果","Price":5.5},{"Name":"橘子","Price":2.5},{"Name":"柿子","Price":16}]}
?//轉載:http://www.rczjp.cn/HTML/101201/20104701014751.htmls
轉載于:https://www.cnblogs.com/leidc/archive/2012/02/29/2373085.html
總結
以上是生活随笔為你收集整理的C#将对象序列化成JSON字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: selenium操作浏览器的前进和后退
- 下一篇: Threading in C#