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;
    }
}
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在Unity中,可以使用JsonUtility类来解析Json格式的数据。对于多层嵌套Json数据,我们可以使用JsonUtility.FromJson方法将其转换为对应的C#对象,然后再通过对象的属性或方法来访问其中的数据。 下面是一个示例Json数据: ``` { "name": "John", "age": 30, "address": { "city": "New York", "state": "NY", "zipcode": "10001" }, "phoneNumbers": [ { "type": "home", "number": "555-1234" }, { "type": "work", "number": "555-5678" } ] } ``` 我们可以定义一个对应的C#类来表示这个Json数据: ``` [Serializable] public class Person { public string name; public int age; public Address address; public List<PhoneNumber> phoneNumbers; } [Serializable] public class Address { public string city; public string state; public string zipcode; } [Serializable] public class PhoneNumber { public string type; public string number; } ``` 然后使用JsonUtility.FromJson方法将Json数据转换为Person对象: ``` string jsonData = "{...}"; // 假设这里是示例Json数据 Person person = JsonUtility.FromJson<Person>(jsonData); ``` 现在我们就可以通过person对象的属性或方法来访问其中的数据了,例如: ``` Debug.Log(person.name); // 输出 "John" Debug.Log(person.address.city); // 输出 "New York" Debug.Log(person.phoneNumbers[0].number); // 输出 "555-1234" ``` 需要注意的是,在使用JsonUtility类时,对象的属性或字段必须是public修饰的,并且类必须使用[Serializable]标记进行标记。此外,Json数据中的键名必须与C#类中的属性或字段名称相对应。如果Json数据中的键名与属性或字段名称不匹配,可以使用[JsonProperty]标记来指定对应关系。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星之擎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值