我要的数据格式:
[{"maintaintime":"2022-04-11 19:28:19","note":"","item":"III-3)导向部分","creator":"1869002(源源)","createtime":"2022-04-11 19:28:19","sortseq":"23","filename":"","factoryid":"9587","description":"导柱,导套,斜导柱,复位杆,二级分模限位杆,顶针板导柱、导套,动定模固定锥面等磨损情况检查、修复、更换","id":"28447","delflag":"0","maintainer":"1869002(源源)","prioritysort":1},{"maintaintime":"2022-04-11 19:28:19","note":"","item":"III-4)顶出部分","creator":"1869002(源源)","createtime":"2022-04-11 19:28:19","sortseq":"24","filename":"","factoryid":"9587","description":"顶针、斜顶及斜顶固定装置是否磨损,顶针孔、斜顶槽是否磨损,顶针板是否变形,如果变形式必须校正","id":"28448","delflag":"0","maintainer":"1869002(源源)","prioritysort":1}]
可以从上一个接口获取的数据:
{"total":19,"pages":19,"hint":"","resultCode":"200","error":"","rows":[{"maintaintime":"2022-04-11 19:28:19.0","note":"","item":"III-6)其他","creator":"1869002(源源)","createtime":"2022-04-11 19:28:19.0","sortseq":"26","filename":"","factoryid":"9587","description":"进浇口、流道或型腔有胶料残留时,应用铜针在进料嘴处敲出,不可用钢针等硬物敲打模具","id":"28450","delflag":"0","maintainer":"1869002(源源)"},{"maintaintime":"2023-08-07 16:42:13.0","note":"","item":"III-5)冷却水路","creator":"1869002(源源)","createtime":"2022-04-11 19:28:19.0","sortseq":"25","filename":"28449_20230807164213.jpg","factoryid":"9587","description":"密封圈老化检查,更换老化密封圈,运水堵头密封性检查,紧固或更换密封不严运水堵头","id":"28449","delflag":"0","maintainer":"18616636949(黄媛媛)"},{"maintaintime":"2022-04-11 19:28:19.0","note":"","item":"III-4)顶出部分","creator":"1869002(源源)","createtime":"2022-04-11 19:28:19.0","sortseq":"24","filename":"","factoryid":"9587","description":"顶针、斜顶及斜顶固定装置是否磨损,顶针孔、斜顶槽是否磨损,顶针板是否变形,如果变形式必须校正","id":"28448","delflag":"0","maintainer":"1869002(源源)"}],"successful":1}
新的数据格式里要求增加键值对 “prioritysort”:“1”,在 Metersphere 的后置脚本里,添加如下脚本
//创建一个String类型变量,接收从接口返回的数据
String response_data = prev.getResponseDataAsString();
//创建一个jsonObject类型对象,将string类型的变量转成jsonObject类型对象
JSONObject data_obj = new JSONObject(response_data);
//提取jsonObject对象里的数组内容
JSONArray rows_list = (JSONArray)((JSONObject)data_obj).get("rows");
//新建一个Json数组
JSONArray spotitem_list = new JSONArray();
log.info("spotitem_list:"+ spotitem_list);//打印出来可以看到是一个空数组
//循环获取数组里的值
for(int i=0;i<rows_list.length();i++){
rows_i = (JSONObject)rows_list.get(i);//依次取出第1、2、3、4、5......i 个对象的值
log.info("rows_i:"+ rows_i);//打印出来可以看到第1、2、3、4、5......i 个对象的值
log.info("rows_i类型:"+ rows_i.getClass());//判断对象的类型,可以用getClass()方法
//依次取出需要的字段
moldid = ((JSONObject)rows_list.get(i)).get("moldid");
itemid = ((JSONObject)rows_list.get(i)).get("id");
groupid = ((JSONObject)rows_list.get(i)).get("groupid");
//依次删除不需要的字段
rows_i.remove("item");
rows_i.remove("prioritysort");
rows_i.remove("description");
rows_i.remove("groupname");
rows_i.put("result","true");
rows_i.put("note","metersphere测试");
log.info("rows_"+i+":"+rows_i);//打印出来可以看到删除后的对象的值
}