JFinalJson.setDefaultConvertDepth(int depth)这个函数用来设置全局默认的转json的层次深度。但测试发现有一个疑惑:
踩坑记:我构造一个10层的树,每个节点还有一个children节点是数组,发现最后一层的children里两个元素变成了children:[null,null],但我看默认的全局解析深度是16层。我明明只有10层。
我设置JFinalJson.setDefaultConvertDepth(17),但渲染出来的只有也不对,设置成18,可以正常渲染到9层,设置20可以正常渲染到10层。这个数是两个两个的增加。
我测试了一个节点有两个key的值都是数组,也是这个规律。
搞迷糊了,麻烦谁懂解释一下
准备数据的代码:
List<Record> recList = new ArrayList<>(); for(int i=1;i<20;i++) { int id = i; int pid = i - 1; String text = "Json"+i; Record rec = new Record(); rec.put("id", id); rec.put("pid", pid); rec.put("text", text); recList.add(rec); } //构造树 List<JSONObject> tree = TreeUtil.buildTreeFromRecord(recList, "id", "id", "text", "text", "pid", 0L, ""); this.renderJson(tree);
其中,TreeUtil.buildTreeFromRecord函数就是根据List<Record>集合用stream递归构造树的静态函数。
当不设置JFinalJson.setDefaultConvertDepth(100);保持默认的时候,渲染出来的截图:
当设置JFinalJson.setDefaultConvertDepth(100);渲染出来的截图:
当设置JFinalJson.setDefaultConvertDepth(17);渲染出来的截图:
当设置JFinalJson.setDefaultConvertDepth(18);渲染出来的截图:
这就是我懵逼的原因!
项目:JFinal
public static void main(String[] args) {
Okv base = null;
Okv dad = null;
String[] kin = {"媳妇", "弟弟", "妹妹"};
List<String> riches = Arrays.asList(new String[]{"房子", "车子", "票子"});
for (int i = 0; i <= 20; i++) {
Okv son = Okv.of("name", "杜" + i);
son.set("kin", kin);
son.set("riches", riches);
if (base == null){
base = son;
}else {
dad.set("son", son);
}
dad = son;
}
JFinalJson.setDefaultConvertDepth(18);
String json = JFinalJson.getJson().toJson(base);
System.out.println(json);
}
{"name":"杜0","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜1","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜2","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜3","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜4","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜5","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜6","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜7","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜8","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜9","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜10","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜11","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜12","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜13","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜14","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜15","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜16","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜17","kin":["媳妇","弟弟","妹妹"],"riches":["房子","车子","票子"],"son":{"name":"杜18","kin":null,"riches":null,"son":null}}}}}}}}}}}}}}}}}}}