递归替换,老蔡牛逼
package com.plus.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.media.sound.SoftTuning;
import sun.reflect.generics.tree.VoidDescriptor;
public class NodeTreeTest {
public static void main(String[] args) {
String node="{\n" +
" \"code\": \"0000\",\n" +
" \"msg\": \"数据响应正确!\",\n" +
" \"data\": [\n" +
" {\n" +
" \"inv\": [\n" +
" {\n" +
" \"FINALENTNAME\": \"深圳市人民政府国有资产监督管理委员会\",\n" +
" \"POSITION\": \"\",\n" +
" \"FINALACCONAM\": \"-\",\n" +
" \"CAPITALCHAIN\": \"深圳市人民政府国有资产监督管理委员会 -> 深圳市地铁集团有限公司 -> 万科企业股份有限公司\",\n" +
" \"FINALSUBCONAM\": \"-\",\n" +
" \"FINALRATIO\": \"27.61%\"\n" +
" },\n" +
" {\n" +
" \"FINALENTNAME\": \"郁亮\",\n" +
" \"POSITION\": \"法人\",\n" +
" \"FINALACCONAM\": \"-\",\n" +
" \"CAPITALCHAIN\": \"郁亮 -> 万科企业股份有限公司\",\n" +
" \"FINALSUBCONAM\": \"-\",\n" +
" \"FINALRATIO\": \"\"\n" +
" }\n" +
" ],\n" +
" \"node\": {\n" +
" \"name\": \"万科企业股份有限公司\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"1.26\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"招商财富-招商银行-德赢1号专项资产管理计划\",\n" +
" \"real_conprop\": \"1.26\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"1.14\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"中国证券金融股份有限公司\",\n" +
" \"real_conprop\": \"1.14\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"3.77\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"深圳盈嘉众实业合伙企业(有限合伙)\",\n" +
" \"real_conprop\": \"3.77\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 2,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"深圳盈安财务顾问企业(有限合伙)\",\n" +
" \"real_conprop\": \"3.77\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"99.72\",\n" +
" \"address\": \"\",\n" +
" \"level\": 3,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"华能贵诚信托有限公司\",\n" +
" \"real_conprop\": \"3.75944\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"67.92\",\n" +
" \"address\": \"\",\n" +
" \"level\": 4,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"华能资本服务有限公司\",\n" +
" \"real_conprop\": \"2.55341\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"61.22\",\n" +
" \"address\": \"\",\n" +
" \"level\": 5,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"中国华能集团有限公司\",\n" +
" \"real_conprop\": \"1.5632\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 6,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"国务院国有资产监督管理委员会\",\n" +
" \"real_conprop\": \"1.5632\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"31.48\",\n" +
" \"address\": \"\",\n" +
" \"level\": 4,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"贵州乌江能源投资有限公司\",\n" +
" \"real_conprop\": \"1.18347\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 5,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"贵州乌江能源集团有限责任公司\",\n" +
" \"real_conprop\": \"1.18347\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"1.63\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"中央汇金资产管理有限责任公司\",\n" +
" \"real_conprop\": \"1.63\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 2,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"中央汇金投资有限责任公司\",\n" +
" \"real_conprop\": \"1.63\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 3,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"中国投资有限责任公司\",\n" +
" \"real_conprop\": \"1.63\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 4,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"国务院\",\n" +
" \"real_conprop\": \"1.63\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"4\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"16.35\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"HKSCC NOMINEES LIMITED\",\n" +
" \"real_conprop\": \"16.35\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"27.61\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"深圳市地铁集团有限公司\",\n" +
" \"real_conprop\": \"27.61\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 2,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"深圳市人民政府国有资产监督管理委员会\",\n" +
" \"real_conprop\": \"27.61\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"1.05\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"深圳盈安财务顾问企业(有限合伙)\",\n" +
" \"real_conprop\": \"1.05\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"99.72\",\n" +
" \"address\": \"\",\n" +
" \"level\": 2,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"华能贵诚信托有限公司\",\n" +
" \"real_conprop\": \"1.04706\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"67.92\",\n" +
" \"address\": \"\",\n" +
" \"level\": 3,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"华能资本服务有限公司\",\n" +
" \"real_conprop\": \"0.71116\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"61.22\",\n" +
" \"address\": \"\",\n" +
" \"level\": 4,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"中国华能集团有限公司\",\n" +
" \"real_conprop\": \"0.43537\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 5,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"国务院国有资产监督管理委员会\",\n" +
" \"real_conprop\": \"0.43537\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"31.48\",\n" +
" \"address\": \"\",\n" +
" \"level\": 3,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"贵州乌江能源投资有限公司\",\n" +
" \"real_conprop\": \"0.32961\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [\n" +
" {\n" +
" \"conprop\": \"100.0\",\n" +
" \"address\": \"\",\n" +
" \"level\": 4,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"贵州乌江能源集团有限责任公司\",\n" +
" \"real_conprop\": \"0.32961\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" },\n" +
" {\n" +
" \"conprop\": \"5.27\",\n" +
" \"address\": \"\",\n" +
" \"level\": 1,\n" +
" \"certype\": \"\",\n" +
" \"idcard\": \"\",\n" +
" \"name\": \"香港中央结算有限公司\",\n" +
" \"real_conprop\": \"5.27\",\n" +
" \"position\": \"\",\n" +
" \"subconam\": \"\",\n" +
" \"subnode\": [],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" ],\n" +
" \"tags\": \"1\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(node);
JSONArray data = jsonObject.getJSONArray("data");
JSONObject jsonObject1 = data.getJSONObject(0);
JSONObject node1 = jsonObject1.getJSONObject("node");
JSONObject result = new JSONObject();
result.put("children", new JSONArray());
test(result.getJSONArray("children"), node1.getJSONArray("subnode"));
System.out.println(result.toJSONString());
}
public static void test(JSONArray result, JSONArray jsonArr1) {
if (jsonArr1.isEmpty()) {
return;
}
for (int i = 0; i < jsonArr1.size(); i ++) {
JSONObject jb1 = (JSONObject) jsonArr1.get(i);
JSONObject jsonObject = new JSONObject();
jsonObject.put("nodeName", jb1.get("name"));
jsonObject.put("isEnt", jb1.get("tags"));
jsonObject.put("nodeType", jb1.get("conprop"));
jsonObject.put("children", new JSONArray());
result.add(jsonObject);
test((JSONArray) jsonObject.get("children"), (JSONArray) jb1.get("subnode"));
}
}
}
|