JSON快速取值(JAVA)
因为工作需要,经常要从复杂的json里面取值,每次要做写一堆JSONObject,然后我就写了一个小工具(刚毕业,写的很菜)。
工具代码
用了hutool的格式转换(Convert)、正则工具(ReUtil)和JSON工具
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.json.*;
import org.junit.Test;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
public class JSONValuesUtil {
/**
* 获得价值
* 若传入数组,表达式应为:[0].FDetailID.FDETAILID__FFLEX5.FNumber (tip:传入数组 第一个元素应为 【数字】)
* 若传入json对象,表达式应为:Model.FEntity[0].FDetailID.FDETAILID__FFLEX5.FNumber
* @param queue 队列
* @param jsonStr json str
* @return {@link String}
*/
private static String getValue(Queue<String> queue, String jsonStr) {
//如果队列为空就直接返回null
if (queue.size() == 0) {
return null;
}
JSONTokener jsonTokener = new JSONTokener(jsonStr,JSONConfig.create());
Object json = jsonTokener.nextValue();
//获取头部属性
String poll = queue.poll();
// 判断json类型
if (json instanceof JSONArray) {
JSONArray jsonArray = (JSONArray) json;
List<String> all = ReUtil.findAll("\\[(\\d+)\\]", poll, 1);
if (all.size() > 0) {
//如果是数组
String index = all.get(0);
// 判断当前队列是否有值 有就递归没有就返回jsonString
String str = jsonArray.getStr(Convert.toInt(index));
if (queue.peek() != null) {
return getValue(queue, str);
} else {
return str;
}
}
return null;
} else {
JSONObject jsonObject = (JSONObject) json;
List<String> all = ReUtil.findAll("\\[(\\d+)\\]", poll, 1);
if (all.size() > 0) {
//如果是数组
String index = all.get(0);
// 去除数组字段 (【0】)
poll = poll.substring(0, poll.length() - index.length() - 2);
// 判断当前队列是否有值 有就递归没有就返回jsonString
if (queue.peek() != null) {
return getValue(queue,jsonObject.getJSONArray(poll).getStr(Convert.toInt(index)));
} else {
return jsonObject.getJSONArray(poll).getStr(Convert.toInt(index));
}
} else {
// 如果是JSONObject
// 判断当前队列是否有值 有就递归没有就返回jsonString
if (queue.peek() != null) {
return getValue(queue,jsonObject.getStr(poll));
} else {
return jsonObject.getStr(poll);
}
}
}
}
/**
* 格式
*
* @param json json字符串
* @param formatStr 格式化字符串(a.b[0].c)
* @return {@link String}
*/
public static String format(String json, String formatStr) {
String[] attributes = formatStr.split("\\.");
if (attributes.length == 0) {
return null;
}
// 把数组转成队列(方便操作)
Queue<String> queue = new ArrayDeque<>(Arrays.asList(attributes));
return getValue(queue, json);
}
/**
* 测试
*/
@Test
public void test() {
String json = "{\"IsDeleteEntry\":\"true\",\"IsVerifyBaseDataField\":\"false\",\"IsEntryBatchFill\":\"true\",\"ValidateFlag\":\"true\",\"NumberSearch\":\"true\",\"IsAutoSubmitAndAudit\":\"false\",\"Model\":{\"FAccountBookID\":{\"FNumber\":\"101\"},\"FBUSDATE\":\"2021-06-30\",\"FDate\":\"2021-06-30\",\"FVOUCHERGROUPID\":{\"FNumber\":\"PZZ5\"},\"FYEAR\":2021,\"FDocumentStatus\":\"Z\",\"FISADJUSTVOUCHER\":false,\"FPERIOD\":6,\"FEntity\":[{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转入人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0007\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00149\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转入人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01067\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转入人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01071\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转入人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0005\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01113\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转入人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SY.00081\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":5,\"FDEBIT\":5},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转入人数\",\"FACCOUNTID\":{\"FNumber\":\"7777\"},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":11,\"FCREDIT\":11},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00015\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":5,\"FDEBIT\":5},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0004\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.001017\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0007\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00149\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01057\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01058\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01065\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01071\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0005\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01088\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.02\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01103\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区转出人数\",\"FACCOUNTID\":{\"FNumber\":\"7777\"},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":17,\"FCREDIT\":17},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区复课人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00015\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区复课人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0008\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01038\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区复课人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01074\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区复课人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01077\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区复课人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0008\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01021\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区复课人数\",\"FACCOUNTID\":{\"FNumber\":\"7777\"},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":6,\"FCREDIT\":6},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00015\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00245\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":4,\"FDEBIT\":4},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0004\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00874\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00888\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01027\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0007\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01040\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01074\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0004\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01079\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0008\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01021\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0008\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01056\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01063\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01065\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01100\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.03\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SY.00081\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区休学人数\",\"FACCOUNTID\":{\"FNumber\":\"7777\"},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":21,\"FCREDIT\":21},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00015\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":3,\"FDEBIT\":3},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0007\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00149\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0005\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00852\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0015\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.00998\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01027\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01074\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0004\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01079\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0008\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01090\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FDEBIT\":2},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0004\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01097\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0008\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01056\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01065\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01072\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01073\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0004\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01084\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01091\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01100\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01103\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":3,\"FDEBIT\":3},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"607.02.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SY.00081\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":3,\"FDEBIT\":3},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实增人数\",\"FACCOUNTID\":{\"FNumber\":\"7777\"},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":26,\"FCREDIT\":26},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实减人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0001\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ.01077\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实减人数\",\"FACCOUNTID\":{\"FNumber\":\"607.03.01\"},\"FDetailID\":{\"FDETAILID__FFLEX5\":{\"FNumber\":\"04.01.008\"},\"FDETAILID__FF100005\":{\"FNumber\":\"0009\"},\"FDETAILID__FFLEX7\":{\"FNumber\":\"SJ01092\"}},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":1,\"FDEBIT\":1},{\"FEXPLANATION\":\"石家庄区域2021年6月各校区实减人数\",\"FACCOUNTID\":{\"FNumber\":\"7777\"},\"FCURRENCYID\":{\"FNumber\":\"PRE001\"},\"FEXCHANGERATETYPE\":{\"FNumber\":\"HLTX01_SYS\"},\"FAMOUNTFOR\":2,\"FCREDIT\":2}]}}\n";
String formatStr = "Model.FEntity[0].FDetailID.FDETAILID__FFLEX5.FNumber";
String format = format(json, formatStr);
System.out.println(format);
}
}
再附加个注解 用于识别实体类属性上注解的json路径 并直接根据路径获取值
注解
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
// 因为是搞金蝶的 所以名字就直接带了个金蝶
public @interface KingdeeJsonPath {
String path();
}
附加解析的方法
// 只需要传入json 和 new 出来的新对象就行
/**
PurchaseOrder p =new PurchaseOrder();
String a="{'Result':{'Result':{Id:'123123',BillNo:'223223'}}}";
formatToBean(a, p);
// 数据就会直接赋值进 P 对象里面
**/
public <T> void formatToBean(String json, T t) {
//获取传入的实体类的属性
Field[] fields = t.getClass().getDeclaredFields();
// 迭代属性
for (Field field : fields) {
// 判断属性上是否有 @KingdeeJsonPath 的注解
if (field.isAnnotationPresent(KingdeeJsonPath.class)) {
// 过去注解中的path 属性
KingdeeJsonPath declaredAnnotation = field.getDeclaredAnnotation(KingdeeJsonPath.class);
// 根据属性上的值 直接调用上面的工具 获取值
String format = JSONValuesUtil.format(json, declaredAnnotation.path());
if (StrUtil.isNotEmpty(format)) {
// 激活属性
field.setAccessible(true);
try {
// 直接给属性赋值
field.set(t, Convert.convert(field.getType(), format));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
实体类
public class PurchaseOrder {
/**
* id
*/
@KingdeeJsonPath(path = "Result.Result.Id")
private Integer id;
/**
* 编码
*/
@KingdeeJsonPath(path = "Result.Result.BillNo")
private String BillNo;
}