zoukankan      html  css  js  c++  java
  • JSON数组序列化C#方法

            /// <summary>  
            /// dataTable转换成Json格式 JSON对应关系 三层数组
            /// </summary>  
            /// <param name="dt">需要转换的表格</param>  
            /// <returns></returns>  
            public static string Table2JsonAryAbNormallData(DataTable phenonmenondt, string station)
            {
                string json = string.Empty;
                if (phenonmenondt.Rows.Count <= 0)
                {
                    return "{"total":" + 0 + ","meta":{"status":201,"msg":"无数据"}}";
                }
                List<Phenomenon_Model> phelist = new List<Phenomenon_Model>();
                for (int i = 0; i < phenonmenondt.Rows.Count; i++)
                {
                    List<Reason1_Model> reason1list = new List<Reason1_Model>();
                    Phenomenon_Model phe = new Phenomenon_Model();
                    phe.id = i;
                    phe.value3 = phenonmenondt.Rows[i][0].ToString();
                    DataTable reason1dt = SqlHelper.Query(sql语句);
                    for (int j = 0; j < reason1dt.Rows.Count; j++)
                    {
                        List<Reason2_Model> reason2list = new List<Reason2_Model>();
                        Reason1_Model rea1 = new Reason1_Model();
                        rea1.id = j;
                        rea1.value4 = reason1dt.Rows[j][0].ToString();
                        DataTable reason2dt = SqlHelper.Query(sql语句);
                        for (int n = 0; n < reason2dt.Rows.Count; n++)
                        {
                            Reason2_Model rea2 = new Reason2_Model();
                            rea2.id = n;
                            rea2.value5 = reason2dt.Rows[n][0].ToString();
                            reason2list.Add(rea2);
                        }
                        rea1.Children = reason2list;
                        reason1list.Add(rea1);
                    }
                    phe.Children = reason1list;
                    phelist.Add(phe);
                }
    
                json = Newtonsoft.Json.JsonConvert.SerializeObject(phelist);//序列化对象
                json = "{" + ""data":" + json + "," + ""meta": {"msg": "获取成功","status": 200}" + "}";
                return json;
            }
     public class Phenomenon_Model
        {
            /// <summary>
            /// ID编号
            /// </summary>
            public int id { get; set; }
            /// <summary>
            /// 现象
            /// </summary>
            public string value3 { get; set; }
            /// <summary>
            /// 子节点
            /// </summary>
            public List<Reason1_Model> Children { get; set; }
        }
     public class Reason1_Model
        {
            /// <summary>
            /// 编号ID
            /// </summary>
            public int id { get; set; }
            /// <summary>
            /// 原因1
            /// </summary>
            public string value4 { get; set; }
            /// <summary>
            /// 子节点
            /// </summary>
            public List<Reason2_Model> Children { get; set; }
        }
     public class Reason2_Model
        {
            /// <summary>
            /// ID
            /// </summary>
            public int id { get; set; }
            /// <summary>
            /// Reason2
            /// </summary>
            public string value5 { get; set; }
        }

    1层JSON

        List<RefillData_Model> list = new List<RefillData_Model>();//List存数据
    DataTable refilldt = fill.GetFacInfo();
                            for (int j = 0; j < refilldt.Rows.Count; j++)
                            {
                                RefillDataFac_Model refill = new RefillDataFac_Model();
                                refill.action = refilldt.Rows[j]["action"].ToString();
                                refill.facility = refilldt.Rows[j]["facility"].ToString();
                                refill.hSerial = refilldt.Rows[j]["Head_serial"].ToString();
                                refill.so = refilldt.Rows[j]["so"].ToString();
                                refill.refno = refilldt.Rows[j]["refno"].ToString();
                                refill.partNum = refilldt.Rows[j]["partno"].ToString();
                                refill.slot = refilldt.Rows[j]["slot"].ToString();
                                refill.qty = refilldt.Rows[j]["planqty"].ToString();
                                refill.Sdte = refilldt.Rows[j]["sdte"].ToString();
                                refill.stme = refilldt.Rows[j]["stme"].ToString();
                                refill.reelcut = refilldt.Rows[j]["reelcut"].ToString();
                                refill.mdte = refilldt.Rows[j]["mdte"].ToString();
                                refill.mtme = refilldt.Rows[j]["mtme"].ToString();
                                refill.soseq = refilldt.Rows[j]["soseq"].ToString();
                                refill.slotserial = refilldt.Rows[j]["SlotSerial"].ToString();
                                refilldata.Add(refill);
                            }
                            string json = JsonConvert.SerializeObject(refilldata);

    以上是3层和1层的JSON数组写法 通过NewtonSoftJson 当然也可以用JSONhelper里面的拼接方法 在需要使用灵活的JSON时可以自己拼接。

    反序列化

                List<AbNormallReport_Model> datalist = new List<AbNormallReport_Model>();
                datalist = Newtonsoft.Json.JsonConvert.DeserializeObject<List<AbNormallReport_Model>>(data);
  • 相关阅读:
    A % B Problem
    封锁阳光大学
    数楼梯
    海滩防御
    修复公路
    四子连棋
    口袋的天空
    兔子数
    逆序对&求逆序对
    【模板】单源最短路径*
  • 原文地址:https://www.cnblogs.com/cdjbolg/p/12376782.html
Copyright © 2011-2022 走看看