C#如何解析嵌套json字符串? 下面我们来看下复杂Json解析方法教程 


json格式比较复杂,如何根据json快速生成Model?
手写Model是比较费事的,下面推荐一个自动生成model工具


Json生成C#实体类工具


注:此序列化需要JsonHelper的支持,点击下载JsonHelper帮助类库

点击查看JsonHelper帮助类库用法教程


看不太直观吗?直接点击下载demo运行一下吧!!不明白的可以加群问站长



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QTJson
{
    public partial class JsonTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //c#如何解析嵌套json?
                string qtjson = "{\"usr_code\":\"testuser1\",\"indate\":\"2016-01-27 14:15:27\",\"p_order\":{\"p_order_no\":\"122\",\"p_client\":\"AAA\",\"p_order_items\":[{\"p_product_id\":\"1\",\"product_id\":\"21\",\"remark\":\"XXXX\",\"p_num\":\"10\",\" a_num \":\"9\"},{\"p_product_id\":\"1\",\"product_id\":\"21\",\"remark\":\"测试系统\",\"p_num\":\"10\",\" a_num \":\"9\"}]}}";
                P_order P_orderInfo = new P_order();//接收json字符串中p_order的数据
                List<P_order_itemsItem> P_order_itemsItemList = new List<P_order_itemsItem>();//接收json字符串中p_order_items的数据
                QTJson qtinfo = (QTJson)JsonHelper.jsonDes<QTJson>(qtjson);
                if (qtinfo != null)
                {
                    P_orderInfo = qtinfo.p_order;
                    foreach (var item in P_orderInfo.p_order_items)
                    {
                        P_order_itemsItem porderinfo = new P_order_itemsItem()
                        {
                            p_product_id = item.p_product_id,
                            product_id = item.product_id,
                            remark = item.remark,
                            p_num = item.p_num,
                            a_num = item.a_num
                        };
                        P_order_itemsItemList.Add(porderinfo);
                    }
                    //此时 P_orderInfo==>p_order中的数据
                    //P_order_itemsItemList==>p_order_items中的数据
                    //接下来处理接收到的数据
                    string p_order_itemsstr = string.Empty;
                    foreach (var porderitem in P_order_itemsItemList)
                    {
                        p_order_itemsstr  = porderitem.remark   "  ,   ";
                    }
                    Response.Write("最外层json数据接收:"   qtinfo.usr_code   "<br>p_order中的数据接收:"   P_orderInfo.p_order_no   "<br>p_order_items中数据接收:"   p_order_itemsstr);
                }
            }
        }
        #region C#嵌套json 测试model
        public class P_order_itemsItem
        {
            /// <summary>
            /// 
            /// </summary>
            public string p_product_id { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string product_id { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string remark { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string p_num { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string a_num { get; set; }

        }
        public class P_order
        {
            /// <summary>
            /// 
            /// </summary>
            public string p_order_no { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string p_client { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public List<P_order_itemsItem> p_order_items { get; set; }

        }
        public class QTJson
        {
            /// <summary>
            /// 
            /// </summary>
            public string usr_code { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public string indate { get; set; }

            /// <summary>
            /// 
            /// </summary>
            public P_order p_order { get; set; }

        }
        #endregion
    }
}