Same level of list data into multi-level json

Internet search a lot of information still have not found what I wanted, and then wrote a front-end js recursive data will show up.

Raw data

After the data in the database query is out of this format, in order that the multi-level data into json data format to determine the relationship between parent and child nodes based on the id and parentID therein.

getJsonTree function = var (JSONView, the parentId) { 
var itemArr = {};
var = In Flag to true; // switch statement
for (var I = 0; I <jsonView.length; I ++) {
var Node = JSONView [I];
IF (== node.parentId the parentId) {
In Flag to false =;
var = node.fieldName the fieldName;
var = getJsonTree the newNode (JSONView, node.id);
itemArr [the fieldName] = the newNode;
}
}
IF (In Flag) {
return null;
}
return [itemArr];
}
using a recursive algorithm to traverse all of the data is formed out of the multi-level json, then a third-party plug-ins can use its data format jsonView.js

This method is not very good, because I have more than four pieces of data so that each piece of data to traverse it again, so there is a big problem, and that is the problem traversal time, this uses a time for space , resulting in a great waste of time.

The second method I ask one of my seniors, using java to write a recursive algorithm will turn into a multi-level json.

public String list(ModelMap modelMap) {
List<IcdsOriginalField> icdsOriginalFields = icdsOriginalFieldService.selectJson();

Map<String, IcdsOriginalField> parents = new HashMap<>();
  Map<String, List<IcdsOriginalField>> childs = new HashMap<>();
if(icdsOriginalFields!=null && icdsOriginalFields.size()>0) {
for(IcdsOriginalField item : icdsOriginalFields) {
parents.put(item.getId().toString(), item);
String pid = item.getParentId() != null ? item.getParentId().toString(): "0";
if(childs.get(pid) != null ) {
childs.get(pid).add(item);
}else {
List<IcdsOriginalField> temp = new ArrayList<>();
temp.add(item);
childs.put(pid,temp);
}
}
}
List<Object> json = diGuiOpt("0", childs);
//
SerializerFeature.WriteMapNullValue This is the time to address data is empty continue to write "null"
  modelMap.put ( "jsonView", JSONObject.toJSONString ( json, SerializerFeature.WriteMapNullValue)); // this is a return to the front page of data to 
}
method basically Thus, when the front and rear ends of data transmission according to their own programs to decide
public List<Object> diGuiOpt(String pid, Map<String, List<IcdsOriginalField>> childrens){
List<IcdsOriginalField> childs = childrens.get(pid);
Map<String, Object> temp = null;
if(childs != null && childs.size() > 0) {
temp = new HashMap<>();
for (IcdsOriginalField item : childs) {
String id = item.getId().toString();
if(childrens.get(id)!= null && childrens.get(id).size()>0) {
temp.put(item.getFieldName(),diGuiOpt(id, childrens));
} else {
temp.put(item.getFieldName(),null);
}
}
} else {
return null;
}
List<Object> result = new ArrayList<>();
result.add(temp);
return result;
}

 

Guess you like

Origin www.cnblogs.com/charleswang-1/p/11411529.html