C#拼装JSON数组简易方法

  下面是我们想要拼接出来的JSON字符串,返回给前台

 {"success":"true","msg":"","data":[{"macName":"正面预拼装机","state":1.0},{"macName":"正面拼板压力架","state":1.0},{"macName":"板片翻身系统","state":1.0},{"macName":"反面预拼装机","state":1.0},{"macName":"反面拼板压力架","state":1.0},{"macName":"划线喷码机","state":0.0},{"macName":"纵骨焊接安装辊道","state":0.0},{"macName":"纵骨焊接机","state":1.0},{"macName":"T排焊接安装辊道","state":0.0}]}

  想看拼接后的JSON格式,这里可以推荐一个网站:https://www.json.cn/

  之前都是用StringBuider手动拼接出JSON数据,拼出来的结果还容易错,下面推荐一个简单方式

  首先,我们把JSON数组创建实体类,那么问题来了,这个实体类如何创建,这里推荐一个网站:http://www.bejson.com/convert/json2csharp/,只需要将JSON格式放入,就会帮我们自动转成实体类

  实体类也比较简单,用到了List集合存放对象,下面Demo演示

实体类

     public class MacState
     {
         /// <summary>
         /// 请求状态
         /// </summary>
         public string success { get; set; }
         /// <summary>
         /// 错误信息
         /// </summary>
         public string msg { get; set; }
         /// <summary>
         /// JSON数组
         /// </summary>
         public List<MacStateData> data { get; set; }
     }
     public class MacStateData
     {
         /// <summary>
         /// 设备名称
         /// </summary>
         public string macName { get; set; }
         /// <summary>
         /// 运行状态 运行/停止(1/0)
         /// </summary>
         public decimal state { get; set; }
     }

函数(标红色的注意!!!)

     /// <summary>
     /// 设备状态情况
     /// </summary>
     /// <returns>JSON数组</returns>
     [WebMethod(Description ="设备状态")]
     public string GetMacState()
     {
         MacState obj = new MacState();
         try
         {            
             DateTime NowDate = DateTime.Now;
             DataTable dt_his_count = new DataTable();
             DataTable dt_mac_state = new DataTable();
             int max_count = 200000;            
             string add_his_sql = "INSERT INTO EMES_MAC_STATE_PORT_HIS (DEF_DATE) VALUES (TO_DATE('{0}','YYYY-MM-DD HH24:MI:SS'))";
             DbUtil.ExecuteSql(string.Format(add_his_sql, NowDate));
             string query_his_sql = "SELECT COUNT(*) FROM EMES_MAC_STATE_PORT_HIS";
             dt_his_count = DbUtil.QueryDT(query_his_sql);
             if (Convert.ToDecimal(dt_his_count.Rows[0][0]??"0")>max_count) //接口日志超过20W条,自动删除
             {
                 string del_sql = "DELETE FROM EMES_MAC_STATE_PORT_HIS";
                 DbUtil.ExecuteSql(del_sql);
             }
             string query_mac_state_sql = "SELECT MAC_NAME,STATE FROM EMES_MAC_STATE";
             dt_mac_state = DbUtil.QueryDT(query_mac_state_sql);
             if (dt_mac_state.Rows.Count > 0)
             {
                 obj.success = "true";
                 obj.msg = "";
                 obj.data = new List<MacStateData>(); //实例化JSON数组,不实例化要报错                
                 for (int i = 0; i < dt_mac_state.Rows.Count; i++)
                 {
                     MacStateData objData = new MacStateData(); //实例化JSON数组对象,用于添加JSON数组集合
                     objData.macName = dt_mac_state.Rows[i]["MAC_NAME"].ToString();
                     objData.state = Convert.ToDecimal(dt_mac_state.Rows[i]["STATE"] ?? "0");
                     obj.data.Add(objData); //添加JSON数组集合
                 }
                 string res = JsonConvert.SerializeObject(obj); //转JSON数组,演示用,下面讲方法使用
                  return JSONHelper.ToJSON(obj); //调用的封装函数,不用管,我们将上面的方式
             }
             else
             {
                 obj.success = "false";
                 obj.msg = "当前无数据信息,请稍后再试!";
                 obj.data = new List<MacStateData>();
                 return JSONHelper.ToJSON(obj);
             }
         }
         catch (Exception ex)
         {
             obj.success = "false";
             obj.msg = ex.Message;
             obj.data = new List<MacStateData>();
             return JSONHelper.ToJSON(obj);
         }
     }

注:将对象转JSON数组,我们需要引入外部类库:Newtonsoft.Json.dll,没有的小伙伴请到我另外一篇博客下载:https://www.cnblogs.com/chenyanbin/p/11200415.html

 引入命名空间:
 using Newtonsoft.Json;
 
 方法使用:
 JsonConvert.SerializeObject(实体类对象)

JSON数组

搞定~~

posted @ 2019-07-18 10:31  陈彦斌  阅读(12300)  评论(0编辑  收藏  举报