gpt4 book ai didi

java - 用于构造多级树的 JSON

转载 作者:太空宇宙 更新时间:2023-11-04 13:55:13 25 4
gpt4 key购买 nike

我想像这样形成 JSON:

{
"Schedule": [
{
"id": "A",
"name": "Summary",
"ischild": "1",
"level1": [
{
"id": "A.1",
"name": "A.1",
"ischild": "1",
"level2": [
{
"id": "A.1.a",
"name": "Income Statement",
"ischild": "0"
},
{
"id": "A.1.b",
"name": "Balance Sheet",
"ischild": "0"
},
{
"id": "A.1.c",
"name": "A.1.c",
"ischild": "1",
"level3": [
{
"id": "A.1.c.1",
"name": "General RWA",
"ischild": "0"
},
{
"id": "A.1.c.2",
"name": "Standardized RWA",
"ischild": "0"
},
{
"id": "A.1.c.3",
"name": "Advanced RWA",
"ischild": "0"
}
]
}
]
}
]
}
]
}

但是我的代码给出以下输出:

{
"Schedule": [
{
"name": "Summary",
"ischild": "1",
"id": "A",
"N_LEVEL": "1"
},
{
"name": "A.1",
"ischild": "1",
"id": "A.1",
"N_LEVEL": "2"
},
{
"name": "Income Statement",
"ischild": "0",
"id": "A.1.a",
"N_LEVEL": "3"
},
{
"name": "Balance Sheet",
"ischild": "0",
"id": "A.1.b",
"N_LEVEL": "3"
},
{
"name": "A.1.c",
"ischild": "1",
"id": "A.1.c",
"N_LEVEL": "3"
},
{
"name": "General RWA",
"ischild": "0",
"id": "A.1.c.1",
"N_LEVEL": "4"
},
{
"name": "Standardized RWA",
"ischild": "0",
"id": "A.1.c.2",
"N_LEVEL": "4"
},
{
"name": "Advanced RWA",
"ischild": "0",
"id": "A.1.c.3",
"N_LEVEL": "4"
}
]
}

这是我的代码:

public static String getJSONFromResultSet(ResultSet rs,String keyName) 
{
System.out.println(" in getJSONFromResultSet method");
Map json = new HashMap();
List list = new ArrayList();
if(rs!=null)
{
try
{
ResultSetMetaData metaData = rs.getMetaData();
while(rs.next())
{
Map<String,Object> columnMap = new HashMap<String, Object>();
for(int columnIndex=1;columnIndex<=metaData.getColumnCount();columnIndex++)
{
if(rs.getString(metaData.getColumnName(columnIndex))!=null)
columnMap.put(metaData.getColumnLabel(columnIndex),rs.getString(metaData.getColumnName(columnIndex)));
else
columnMap.put(metaData.getColumnLabel(columnIndex), "");
}
list.add(columnMap);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
json.put(keyName, list);
}
return JSONValue.toJSONString(json);

最佳答案

我认为如果每个级别的名称都没有改变,您的目标结构可能会更好。级别编号是一个,而不是ischild 也没有任何意义,我认为这是 isNotALeaf,好吧,这是可以解决的,所以也将其保留,所以我们有:

{
"id": "A",
"name": "Summary",
"level": "1",
"children": [
{
"id": "A.1",
"name": "A.1",
"level": "2",
"children": [
{
"id": "A.1.a",
"name": "Income Statement",
"level": "3"
},
{
"id": "A.1.b",
"name": "Balance Sheet",
"level": "3"
}
]
}
}

然后根据该类生成一个自引用类,以便在 GSon 中使用:

package com.example;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;

@Generated("org.jsonschema2pojo")
public class Child {

@Expose
private String id;
@Expose
private String name;
@Expose
private String level;
@Expose
private List<Child> children = new ArrayList<Child>();

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLevel() {
return level;
}

public void setLevel(String level) {
this.level = level;
}

public List<Child_> getChildren() {
return children;
}

public void setChildren(List<Child> children) {
this.children = children;
}

}

关于java - 用于构造多级树的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29896975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com