c#多層嵌套Json

1、類+list:

 

 public class OrderInfo
    {
        /// <summary>
        /// 桌臺狀態表
        /// </summary>
        [DataMember]
        public string bi_deskroom_status { get; set; }
        /// <summary>
        /// 銷售主表
        /// </summary>
        [DataMember]
        public string pos_master { get; set; }
        /// <summary>
 

public class pos_master
    {
        [DataMember]
        public string errcode { get; set; }

        [DataMember]
        public string errmsg { get; set; }

        [DataMember]
        public List<pos_detail> p_detail;


        /// <summary>
        /// 銷售明細表
        /// </summary>
        [DataMember]
        public string detail { get; set; }

        /// <summary>
        /// 企業id
        /// </summary>
        [DataMember]
        public string softid { get; set; }

        /// <summary>
        /// 單號
        /// </summary>
        [DataMember]
        public string sheet_no { get; set; }

  orderInfo.pos_master = grammar_ResultB;

 var p_master = Newtonsoft.Json.JsonConvert.DeserializeObject<po_master>(orderInfo.pos_master);
  List<pos_master> master = p_master.TableInfo;

 var p_detail = Newtonsoft.Json.JsonConvert.DeserializeObject<po_detail>(orderInfo.pos_detail);
 List<pos_detail> detail = p_detail.TableInfo;
               
 var p_detailcs = Newtonsoft.Json.JsonConvert.DeserializeObject<po_detailcs>(orderInfo.pos_detailcs);
  List<pos_detailcs> detailcs = p_detailcs.TableInfo;
              
  var p_detailtaste = Newtonsoft.Json.JsonConvert.DeserializeObject<po_detailtaste>(orderInfo.pos_detailtaste);
 List<pos_detailtaste> detailtaste = p_detailtaste.TableInfo;
             

                try
                {
                    if (grammar_ResultB.IndexOf("查詢無數據") > 0)
                    {
                        return dberror.errorStr("40050");
                    }
                    else
                    {
                        foreach (pos_master po in master)
                        {

                            //errcode  errmsg
                            if (po.errcode != "40015")
                            {
                                po.p_detail = null;
                            }
                            else
                            {
                            }

                            po.p_detail = null;
                            if (grammar_ResultC.IndexOf("查詢無數據") > 0)
                            {
                                return dberror.errorStr("40051");
                            }
                            else
                            {
                                foreach (pos_detail pi in detail)
                                {
                                   
                                    if (po.sheet_no == pi.sheet_no)
                                    {
                                        po.p_detail = detail;
                                    }
                                    else
                                    {
                                       
                                    }

                                    if (pi.errcode != "")
                                    {
                                        pi.p_detailcs = null;
                                        pi.p_detailtaste = null;
                                    }
                                    else
                                    {
                                    }

                                    pi.p_detailcs = null;
                                    if (grammar_ResultD.IndexOf("查詢無數據") > 0)
                                    {
                                        pi.p_detailcs = null;
                                       
                                    }
                                    else
                                    {
                                        List<pos_detailcs> detailcs0 = new List<pos_detailcs>();
                                        foreach (pos_detailcs pd in detailcs)
                                        {

                                           // pi.item_money = 0;
                                            if ( pi.sheet_sort == pd.sheet_sort)
                                            {
                                                 detailcs0.Add(pd);
                                                pi.item_money =  Convert.ToDouble(pi.real_amt) + Convert.ToDouble(pi.add_amt);
                                              
                                            }
                                            else
                                            {             
                                            }

                                            if (pd.errcode != "")
                                            {
                                                pd.p_detailtaste = null;
                                            }
                                            else
                                            {
                                            }

                                            pd.p_detailtaste = null;
                                            if (grammar_ResultE.IndexOf("查詢無數據") > 0)
                                            {
                                                //return dberror.errorStr("40053");
                                                pd.p_detailtaste = null;
                                            }
                                            else
                                            {
                                                List<pos_detailtaste> detailtaste0 = new List<pos_detailtaste>();
                                                foreach (pos_detailtaste pe in detailtaste)
                                                {
                                                    
                                                    
                                                    if ( pd.item_id == pe.item_id&& pd.child_sort == pe.sheet_sort)
                                                    {
                                                        detailtaste0.Add(pe);
                                                        pd.item_money = pd.item_money + Convert.ToDouble(pe.add_range);
                                                    }
                                                    else
                                                    {
                                                       
                                                    }

                                                }
                                                pd.p_detailtaste = detailtaste0;
                                               
                                            }
                                        }
                                        pi.p_detailcs = detailcs0;
                                        pi.item_money = pi.item_money;

                                    }

                                    pi.p_detailtaste = null;
                                    if (grammar_ResultE.IndexOf("查詢無數據") > 0)
                                    {
                                        // return dberror.errorStr("40053");
                                        pi.p_detailtaste = null;
                                    }
                                    else
                                    {
                                       // int m = 0;
                                      List<pos_detailtaste> detailtaste0 = new List<pos_detailtaste>();
                                        foreach (pos_detailtaste pf in detailtaste)
                                        {
                                           // List<pos_detailtaste> detailtaste0 = new List<pos_detailtaste>();
                                           

                                            if ( pi.sheet_sort == pf.sheet_sort)
                                            {
                                                detailtaste0.Add(pf);
                                                //pi.item_money + Convert.ToDouble(pf.add_range);
                                            }
                                            else
                                            {
                                               
                                            }
                                        
                                        }
                                        pi.p_detailtaste = detailtaste0;
                                        pi.item_money = Convert.ToDouble(pi.add_amt) + Convert.ToDouble(pi.real_amt);
                                        pi.item_money = pi.item_money;
                                    }
                                }
                            }
                        }
                    }

                      
                }
                catch (Exception ex)
                {
                    
                    return dberror.errorStr("40015");
                    throw ex;
                }

               return Newtonsoft.Json.JsonConvert.SerializeObject(master); //return Jayrock.Json.Conversion.JsonConvert.ExportToString(master);

                }
                catch (Exception ex)
                {

                    #region 異常
                    if (i < 3)
                    {
                        i++;
                        goto InitInfoA;
                    }
                    if (upoint != null) {
                        messenger.gush(upoint);
                    }
                    return dberror.errorStr("40017");
                    throw ex;
                    #endregion

                }

        }

 

2、類+類:

public class UserInfo

{

public string name;

public int age;

public address addr;

}

public class address

{

public string city;

public string province;
}

然後參照1給類for循環賦值

3、轉換成table,表操作,然後轉list

 public static class jsonObject
        {
            #region DataTable 轉換爲Json 字符串
            /// <summary>
            /// DataTable 對象 轉換爲Json 字符串
            /// </summary>
            /// <param name="dt"></param>
            /// <returns></returns>
            public static string ToJson(this DataTable dt)
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數值
                ArrayList arrayList = new ArrayList();
                foreach (DataRow dataRow in dt.Rows)
                {
                    Dictionary<string, object> dictionary = new Dictionary<string, object>();  //實例化一個參數集合
                    foreach (DataColumn dataColumn in dt.Columns)
                    {
                        dictionary.Add(dataColumn.ColumnName, dataRow[dataColumn.ColumnName].ToString());
                    }
                    arrayList.Add(dictionary); //ArrayList集合中添加鍵值
                }
                return javaScriptSerializer.Serialize(arrayList);  //返回一個json字符串
            }
            #endregion

            #region Json 字符串 轉換爲 DataTable數據集合
            /// <summary>
            /// Json 字符串 轉換爲 DataTable數據集合
            /// </summary>
            /// <param name="json"></param>
            /// <returns></returns>
            public static DataTable ToDataTable(this string json)
            {
                DataTable dataTable = new DataTable();  //實例化
                DataTable result;
                try
                {
                    JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                    javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大數值
                    ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
                    if (arrayList.Count > 0)
                    {
                        foreach (Dictionary<string, object> dictionary in arrayList)
                        {
                            if (dictionary.Keys.Count<string>() == 0)
                            {
                                result = dataTable;
                                return result;
                            }
                            if (dataTable.Columns.Count == 0)
                            {
                                foreach (string current in dictionary.Keys)
                                {
                                dataTable.Columns.Add(current, Type.GetType("System.String"));// dictionary[current].GetType());
                                }
                            }
                            DataRow dataRow = dataTable.NewRow();
                            foreach (string current in dictionary.Keys)
                            {
                                dataRow[current] = dictionary[current];
                            }

                            dataTable.Rows.Add(dataRow); //循環添加行到DataTable中
                        }
                    }
                }
            catch (Exception ex)
            {
               //* return dberror.errorStr("40015");
                throw ex;
            }
            result = dataTable;
                return result;
            }
        #endregion

        public static DataTable ListToDataTableA( List<pos_master> collection)
        {
            var props = typeof(pos_master).GetProperties();
            var master_dt = new DataTable();
            master_dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int m = 0; m < collection.Count(); m++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(m), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    master_dt.LoadDataRow(array, true);
                    //master_dt =Convert(array);
                }
            }
            return master_dt;
        }

        /*public static DataTable Convert(string[,] Arrays)
        {
            DataTable dt = new DataTable();

            int a = Arrays.GetLength(0);
            for (int i = 0; i < Arrays.GetLength(1); i++)
            {
                dt.Columns.Add("col" + i.ToString(), typeof(string));
            }

            for (int i1 = 0; i1 < Arrays.GetLength(0); i1++)
            {
                DataRow dr = dt.NewRow();
                for (int i = 0; i < Arrays.GetLength(1); i++)
                {
                    dr[i] = Arrays[i1, i].ToString();
                }
                dt.Rows.Add(dr);
            }

            return dt;

        }*/


        public static DataTable ListToDataTableB(List<pos_detail> collection)
        {
            var props = typeof(pos_master).GetProperties();
            var master_dt = new DataTable();
            master_dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (collection.Count() > 0)
            {
                for (int m = 0; m < collection.Count(); m++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(collection.ElementAt(m), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    master_dt.LoadDataRow(array, true);
                }
            }
            return master_dt;
        }

    

    public static DataTable ListToDataTableC(List<pos_detailcs> collection)
    {
        var props = typeof(pos_master).GetProperties();
        var master_dt = new DataTable();
        master_dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
        if (collection.Count() > 0)
        {
            for (int m = 0; m < collection.Count(); m++)
            {
                ArrayList tempList = new ArrayList();
                foreach (PropertyInfo pi in props)
                {
                    object obj = pi.GetValue(collection.ElementAt(m), null);
                    tempList.Add(obj);
                }
                object[] array = tempList.ToArray();
                master_dt.LoadDataRow(array, true);
            }
        }
        return master_dt;
    }


    public static DataTable ListToDataTableD(List<pos_detailtaste> collection)
    {
        var props = typeof(pos_master).GetProperties();
        var master_dt = new DataTable();
        master_dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
        if (collection.Count() > 0)
        {
            for (int m = 0; m < collection.Count(); m++)
            {
                ArrayList tempList = new ArrayList();
                foreach (PropertyInfo pi in props)
                {
                    object obj = pi.GetValue(collection.ElementAt(m), null);
                    tempList.Add(obj);
                }
                object[] array = tempList.ToArray();
                master_dt.LoadDataRow(array, true);
            }
        }
        return master_dt;
    }
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章