class Node{
private String id
private String parentId
private List<Node>children
public void addChildren(Node child){
getChildren().add(child)
}
public List<Node>getChildren()
public String transformTojson(Node node ,StringBuffer jsonStr){
//递归
if(jsonStr ==null){
StringBuffer jsonStr=new ()
}
if(node.id!=null){
//拼接父节点json串
jsonStr.append(id:node.id,children:#&children)
}
List childNodeList = node.getChildren()
StringBuffer childrenSB = new()
for( Node node :childNodeList){
childrenSB=transformTojson(node)
}
sonStr.replaceFirst("#&children",childrenSB)
return jsonStr
}
}
伪代码大概这样。
transformTojson方法可以不自己写,直接使用第三方工具jar包的json转化方法,比如gjson。
JSONArray 是json数据格式,它下边包含了jsonObject格式,所以你应该先取jsonObject,如:for(int z = 0z <lengz++){
System.out.println("zzzz"+z)
JSONObject json = jsona.getJSONObject(z)
String name = json.get("name").toString
}
你的jsonarray格式要是正确的话就应该可以拿到name值。