Groovy中JSON快速取值的方法
JSON内容
{
"encryptedData":"62d6013f216192dc5e8b97bbf92b7772",
"sourceData":{
"duration":"13",
"enddate":"2022-04-26 14:00",
"wfstatus":"approve",
"appid":"QJ20220400215-1",
"empno":"EA000281",
"startdate":"2022-04-25 08:00",
"lvcode":"事假",
"lvunit":"H"
}
}
取值
import groovy.json.JsonSlurper;
def jsonSlurper = new JsonSlurper()
String jsonStr = "";
//将JSON字符串放进JSON对象
def object = jsonSlurper.parseText(jsonStr)
//取单个值
String appid = object.sourceDate.appid;
//循环取值,object对象逐级向下导航到需要循环遍历取值的位置
object.sourceData.data.each{
Element row = document.getRootElement().getChild('hris').addChild('row');
row.addChild('appid').setValue(it.appid);
row.addChild('empno').setValue(it.empno);
row.addChild('lvcode').setValue(it.lvcode);
row.addChild('startdate').setValue(it.startdate);
row.addChild('enddate').setValue(it.enddate);
row.addChild('duration').setValue(it.duration);
row.addChild('lvunit').setValue(it.lvunit);
row.addChild('reason').setValue(it.reason);
}
评论区