Map<String,Object>中Fastjson提取entrys对应的值
今天在处理接口数据时,需要解析出对方传入的json数据,并需要取出其中一个字段的值来判断,记录下我的步骤,提供参考:
1.json数据准备
{
"hrOrgUnit": "00000000-0000-0000-0000-000000000000CCE7AED4",
"billState": "0",
"number": "20250305-asfoq",
"applyDate": "2025-03-05",
"applier": "sOGnUMynTtCJvT5mm5ZzVIDvfe0=",
"_entityName": "com.kingdee.eas.hr.affair.app.EmpEnrollBizBill",
"entrys": [{
"empNumber": "F002493",
"empName": "张三",
"IDCardNo": "41048219990709103X",
"position": "6666693wQn/CrQLWFYTeIW8xi5HSuYS4=",
"adminOrg": "ODFLt7rTRj++XjDzvF0pW8znrtQ=",
"bizDate": "2025-03-05",
"bizTime": "2025-03-05 00:00:00",
"enrollDate": "2025-03-05",
"nCell": "-15538223884",
"sourceBillType": "400",
"hrBizDefine": "DawAAAApC9DmaL7Z",
"affairActionReason": "1wXho31/Tsi0LhiLKfCQ+JYRae4=",
"variationReason": "sKSMJ3pDTQCtVNKkqB5WnOas36w=",
"empType": "00000000-0000-0000-0000-000000000002A29E85B3",
"useOldNumber": "0",
"enrollAgain": "0",
"isPrimary": "0",
"sex": ""
},
{
"empNumber": "F002494",
"empName": "李四",
"IDCardNo": "41048219990709103X",
"position": "99999993wQn/CrQLWFYTeIW8xi5HSuYS4=",
"adminOrg": "ODFLt7rTRj++XjDzvF0pW8znrtQ=",
"bizDate": "2025-03-05",
"bizTime": "2025-03-05 00:00:00",
"enrollDate": "2025-03-05",
"nCell": "-15538223884",
"sourceBillType": "400",
"hrBizDefine": "DawAAAApC9DmaL7Z",
"affairActionReason": "1wXho31/Tsi0LhiLKfCQ+JYRae4=",
"variationReason": "sKSMJ3pDTQCtVNKkqB5WnOas36w=",
"empType": "00000000-0000-0000-0000-000000000002A29E85B3",
"useOldNumber": "0",
"enrollAgain": "0",
"isPrimary": "0",
"sex": ""
}],
"id": ""
}
上面的是json数据,需要取出 position 字段的值
2.采用 fastjson 的组件库进行处理
上述的json 对应的是 param.get("obj").toString()
// 将JSON字符串解析为JSONObject
JSONObject jsonObject=JSON.parseObject(param.get("obj").toString());
System.out.println(jsonObject);
//获取需要的json数组,将entrys获取
JSONArray jsonArray =jsonObject.getJSONArray("entrys");
System.out.println(jsonArray);
System.out.println(jsonArray.size());
//遍历出entrys里面的数据
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject entry = jsonArray.getJSONObject(i);
System.out.println(entry);
//根据具体的key取值
String position = entry.getString("position");
System.out.println("取出对应的值:"+position);
}
输出效果:
记下心得