C#如何生成嵌套的JSON数据?
C#把Model转换成复杂的Json数据
Json转成Model工具推荐:
注:此序列化需要JsonHelper的支持,点击下载JsonHelper帮助类库
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; //来源:www.yunjson.com 站长 namespace QToJson { public partial class ToJson : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 如何生成嵌套的JSON数据? Product_store Product_storeinfo = new Product_store() { product_id = "103", pcs = "8", pcs1 = "1", pcs2 = "0" }; Page_info Page_infoinfo = new Page_info() { curr_page = "1", total_page = "2", total_size = "30" }; List<Product_store_itemItem> Product_store_itemItemlist = new List<Product_store_itemItem>() { new Product_store_itemItem {product_id="103",valid_until="2016-01-27 00:00:00",pcs="8"}, new Product_store_itemItem {product_id="103",valid_until="2016-01-27 00:00:00",pcs="4"} }; List<string> goods_piclist = new List<string>(); goods_piclist.Add("192.168.0.6/wms/pic1.jpg"); goods_piclist.Add("192.168.0.6/wms/pic2.jpg"); List<ProductsItem> ProductsItemList = new List<ProductsItem>() { new ProductsItem { barcode = "S110711HF001001004000", product_id = "103", goods_name = "七匹狼夹克", client = "宏高", goods_type = "服装", goods_cat = "精品男装", goods_brand = "七匹狼", goods_spec = "中号", goods_pic = goods_piclist, product_store = Product_storeinfo, product_store_item = Product_store_itemItemlist }, }; Proc_result Proc_resultinfo = new Proc_result() { status = "OK", error_code = "", error_message = "", receive_time = "2016-03-22 14:15:28" }; Location Locationinfo = new Location() { barcode = "S110711HF001001004000", lo_code = "AAA01", page_info = Page_infoinfo, products = ProductsItemList }; Root Rootinfo = new Root() { proc_result = Proc_resultinfo, location = Locationinfo }; string retinfoQTstr = JsonHelper.json(Rootinfo);//此时已转换为json字符串,可以返回数据咯 //结果为:{"proc_result":{"status":"OK","error_code":"","error_message":"","receive_time":"2016-03-22 14:15:28"},"location":{"barcode":"S110711HF001001004000","lo_code":"AAA01","page_info":{"curr_page":"1","total_page":"2","total_size":"30"},"products":[{"barcode":"S110711HF001001004000","product_id":"103","goods_name":"七匹狼夹克","client":"宏高","goods_type":"服装","goods_cat":"精品男装","goods_brand":"七匹狼","goods_spec":"中号","goods_pic":["192.168.0.6/wms/pic1.jpg","192.168.0.6/wms/pic2.jpg"],"product_store":{"product_id":"103","pcs":"8","pcs1":"1","pcs2":"0"},"product_store_item":[{"product_id":"103","valid_until":"2016-01-27 00:00:00","pcs":"8"},{"product_id":"103","valid_until":"2016-01-27 00:00:00","pcs":"4"}]}]}} Response.Write(retinfoQTstr); } } #region 嵌套的JSON数据需要的model public class Proc_result { /// <summary> /// /// </summary> public string status { get; set; } /// <summary> /// /// </summary> public string error_code { get; set; } /// <summary> /// /// </summary> public string error_message { get; set; } /// <summary> /// /// </summary> public string receive_time { get; set; } } public class Page_info { /// <summary> /// /// </summary> public string curr_page { get; set; } /// <summary> /// /// </summary> public string total_page { get; set; } /// <summary> /// /// </summary> public string total_size { get; set; } } public class Product_store { /// <summary> /// /// </summary> public string product_id { get; set; } /// <summary> /// /// </summary> public string pcs { get; set; } /// <summary> /// /// </summary> public string pcs1 { get; set; } /// <summary> /// /// </summary> public string pcs2 { get; set; } } public class Product_store_itemItem { /// <summary> /// /// </summary> public string product_id { get; set; } /// <summary> /// /// </summary> public string valid_until { get; set; } /// <summary> /// /// </summary> public string pcs { get; set; } } public class ProductsItem { /// <summary> /// /// </summary> public string barcode { get; set; } /// <summary> /// /// </summary> public string product_id { get; set; } /// <summary> /// 七匹狼夹克 /// </summary> public string goods_name { get; set; } /// <summary> /// 宏高 /// </summary> public string client { get; set; } /// <summary> /// 服装 /// </summary> public string goods_type { get; set; } /// <summary> /// 精品男装 /// </summary> public string goods_cat { get; set; } /// <summary> /// 七匹狼 /// </summary> public string goods_brand { get; set; } /// <summary> /// 中号 /// </summary> public string goods_spec { get; set; } /// <summary> /// /// </summary> public List<string> goods_pic { get; set; } /// <summary> /// /// </summary> public Product_store product_store { get; set; } /// <summary> /// /// </summary> public List<Product_store_itemItem> product_store_item { get; set; } } public class Location { /// <summary> /// /// </summary> public string barcode { get; set; } /// <summary> /// /// </summary> public string lo_code { get; set; } /// <summary> /// /// </summary> public Page_info page_info { get; set; } /// <summary> /// /// </summary> public List<ProductsItem> products { get; set; } } public class Root { /// <summary> /// /// </summary> public Proc_result proc_result { get; set; } /// <summary> /// /// </summary> public Location location { get; set; } } #endregion } }