下面是我们想要拼接出来的JSON字符串,返回给前台
1 {"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演示
实体类
1 public class MacState 2 { 3 /// <summary> 4 /// 请求状态 5 /// </summary> 6 public string success { get; set; } 7 /// <summary> 8 /// 错误信息 9 /// </summary> 10 public string msg { get; set; } 11 /// <summary> 12 /// JSON数组 13 /// </summary> 14 public List<MacStateData> data { get; set; } 15 } 16 public class MacStateData 17 { 18 /// <summary> 19 /// 设备名称 20 /// </summary> 21 public string macName { get; set; } 22 /// <summary> 23 /// 运行状态 运行/停止(1/0) 24 /// </summary> 25 public decimal state { get; set; } 26 }
函数(标红色的注意!!!)
1 /// <summary> 2 /// 设备状态情况 3 /// </summary> 4 /// <returns>JSON数组</returns> 5 [WebMethod(Description ="设备状态")] 6 public string GetMacState() 7 { 8 MacState obj = new MacState(); 9 try 10 { 11 DateTime NowDate = DateTime.Now; 12 DataTable dt_his_count = new DataTable(); 13 DataTable dt_mac_state = new DataTable(); 14 int max_count = 200000; 15 string add_his_sql = "INSERT INTO EMES_MAC_STATE_PORT_HIS (DEF_DATE) VALUES (TO_DATE('{0}','YYYY-MM-DD HH24:MI:SS'))"; 16 DbUtil.ExecuteSql(string.Format(add_his_sql, NowDate)); 17 string query_his_sql = "SELECT COUNT(*) FROM EMES_MAC_STATE_PORT_HIS"; 18 dt_his_count = DbUtil.QueryDT(query_his_sql); 19 if (Convert.ToDecimal(dt_his_count.Rows[0][0]??"0")>max_count) //接口日志超过20W条,自动删除 20 { 21 string del_sql = "DELETE FROM EMES_MAC_STATE_PORT_HIS"; 22 DbUtil.ExecuteSql(del_sql); 23 } 24 string query_mac_state_sql = "SELECT MAC_NAME,STATE FROM EMES_MAC_STATE"; 25 dt_mac_state = DbUtil.QueryDT(query_mac_state_sql); 26 if (dt_mac_state.Rows.Count > 0) 27 { 28 obj.success = "true"; 29 obj.msg = ""; 30 obj.data = new List<MacStateData>(); //实例化JSON数组,不实例化要报错 31 for (int i = 0; i < dt_mac_state.Rows.Count; i++) 32 { 33 MacStateData objData = new MacStateData(); //实例化JSON数组对象,用于添加JSON数组集合 34 objData.macName = dt_mac_state.Rows[i]["MAC_NAME"].ToString(); 35 objData.state = Convert.ToDecimal(dt_mac_state.Rows[i]["STATE"] ?? "0"); 36 obj.data.Add(objData); //添加JSON数组集合 37 } 38 string res = JsonConvert.SerializeObject(obj); //转JSON数组,演示用,下面讲方法使用 39 return JSONHelper.ToJSON(obj); //调用的封装函数,不用管,我们将上面的方式 40 } 41 else 42 { 43 obj.success = "false"; 44 obj.msg = "当前无数据信息,请稍后再试!"; 45 obj.data = new List<MacStateData>(); 46 return JSONHelper.ToJSON(obj); 47 } 48 } 49 catch (Exception ex) 50 { 51 obj.success = "false"; 52 obj.msg = ex.Message; 53 obj.data = new List<MacStateData>(); 54 return JSONHelper.ToJSON(obj); 55 } 56 }
注:将对象转JSON数组,我们需要引入外部类库:Newtonsoft.Json.dll,没有的小伙伴请到我另外一篇博客下载:https://www.cnblogs.com/chenyanbin/p/11200415.html
1 引入命名空间: 2 using Newtonsoft.Json; 3 4 方法使用: 5 JsonConvert.SerializeObject(实体类对象)
JSON数组
搞定~~