Java 从列表/表中使用json创建树
假设我有这样的表/列表,在这种情况下n=3,但n可以是无限的Java 从列表/表中使用json创建树,java,json,tree,Java,Json,Tree,假设我有这样的表/列表,在这种情况下n=3,但n可以是无限的 groupid answerid1 answerid2 answerid(n) 1 3 6 8 1 3 6 9 1 4 7 2 5
groupid answerid1 answerid2 answerid(n)
1 3 6 8
1 3 6 9
1 4 7
2 5
我想使用java创建这样的父/子树json输出
这样做的代码/步骤是什么。我已经浏览了很多标签,但大多数人都在打印输出,而不是递归地为GSON构建hashmap/ArrayList来解析和写入API。另一点是,每个id都有与之关联的其他数据,这些数据必须包含在json输出中。例如,需要将此{groupid:1,text=toyota}替换为{groupid:1}
非常感谢您的任何帮助,因为我是来自SAS的java新手
我得到这样的数据(只是列表的矩阵)
丰田、汽油、紧凑型、花冠您应该创建类来按照所需的结构对数据进行建模。您基本上是想从一些基于行的数据构建层次结构,这很像XML文档,这可能是一个合适的解决方案。但是您让我上钩了,所以我就用以前的方法进行了研究,并得出了以下结论:
public class Test {
public static void main(String[] args)
{
// hierarchical data in a flattened list
String[][] data = {
{"Toyota", "Gas", "Compact", "Corolla"},
{"Toyota", "Gas", "Compact", "Camry"},
{"Toyota", "Hybrid", "Compact", "Prius"},
{"Honda", "Gas", "Compact", "Civic"}
};
TreeManager treeManager = new TreeManager();
for(String[] row : data)
{
// build the path to our items in the tree
List<String> path = new ArrayList<String>();
for(String item : row)
{
// add this item to our path
path.add(item);
// will add it unless an Item with this name already exists at this path
treeManager.addData(treeManager, path);
}
}
treeManager.getData(data[0]).putValue("MPG", 38);
treeManager.getData(data[1]).putValue("MPG", 28);
Gson gson = new Gson();
System.out.println(gson.toJson(treeManager));
}
/**
* This base class provides the hierarchical property of
* an object that contains a Map of child objects of the same type.
* It also has a field - Name
*
*/
public static abstract class TreeItem implements Iterable<TreeItem>{
private Map<String, TreeItem> children;
private String name;
public TreeItem() {
children = new HashMap<String, TreeItem>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addChild(String key, TreeItem data)
{
children.put(key, data);
}
public TreeItem getChild(String key)
{
return children.get(key);
}
public boolean hasChild(String key)
{
return children.containsKey(key);
}
@Override
public Iterator<TreeItem> iterator() {
return children.values().iterator();
}
}
/**
* This is our special case, root node. It is a TreeItem in itself
* but contains methods for building and retrieving items from our tree
*
*/
public static class TreeManager extends TreeItem
{
/**
* Will add an Item to the tree at the specified path with the value
* equal to the last item in the path, unless that Item already exists
*/
public void addData(List<String> path)
{
addData(this, path);
}
private void addData(TreeItem parent, List<String> path)
{
// if we're at the end of the path - create a node
String data = path.get(0);
if(path.size() == 1)
{
// unless there is already a node with this name
if(!parent.hasChild(data))
{
Group group = new Group();
group.setName(data);
parent.addChild(data, group);
}
}
else
{
// pass the tail of this path down to the next level in the hierarchy
addData(parent.getChild(data), path.subList(1, path.size()));
}
}
public Group getData(String[] path)
{
return (Group) getData(this, Arrays.asList(path));
}
public Group getData(List<String> path)
{
return (Group) getData(this, path);
}
private TreeItem getData(TreeItem parent, List<String> path)
{
if(parent == null || path.size() == 0)
{
throw new IllegalArgumentException("Invalid path specified in getData, remainder: "
+ Arrays.toString(path.toArray()));
}
String data = path.get(0);
if(path.size() == 1)
{
return parent.getChild(data);
}
else
{
// pass the tail of this path down to the next level in the hierarchy
return getData(parent.getChild(data), path.subList(1, path.size()));
}
}
}
public static class Group extends TreeItem {
private Map<String, Object> properties;
public Object getValue(Object key) {
return properties.get(key);
}
public Object putValue(String key, Object value) {
return properties.put(key, value);
}
public Group () {
super();
properties = new HashMap<String, Object>();
}
}
}
公共类测试{
公共静态void main(字符串[]args)
{
//扁平列表中的分层数据
字符串[][]数据={
{“丰田”、“天然气”、“紧凑型”、“花冠”},
{“丰田”、“天然气”、“紧凑型”、“凯美瑞”},
{“丰田”、“混合动力”、“紧凑型”、“普锐斯”},
{“本田”、“天然气”、“紧凑型”、“思域”}
};
TreeManager TreeManager=新的TreeManager();
for(字符串[]行:数据)
{
//在树中构建项目的路径
列表路径=新的ArrayList();
用于(字符串项:行)
{
//将此项添加到我们的路径
路径。添加(项);
//将添加它,除非此路径中已存在具有此名称的项
addData(treeManager,路径);
}
}
treeManager.getData(数据[0]).putValue(“MPG”,38);
treemager.getData(data[1]).putValue(“MPG”,28);
Gson Gson=新的Gson();
System.out.println(gson.toJson(treemager));
}
/**
*这个基类提供了
*包含相同类型的子对象映射的对象。
*它还有一个字段名
*
*/
公共静态抽象类TreeItem实现了Iterable{
私人地图儿童;
私有字符串名称;
公树{
children=newhashmap();
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
public void addChild(字符串键、树项数据)
{
children.put(键、数据);
}
公共树Item getChild(字符串键)
{
返回子项。获取(键);
}
公共布尔hasChild(字符串键)
{
返回子项。containsKey(键);
}
@凌驾
公共迭代器迭代器(){
返回children.values().iterator();
}
}
/**
*这是我们的特例,根节点。它本身就是一个树元素
*但包含从树中构建和检索项的方法
*
*/
公共静态类TreeManager扩展TreeItem
{
/**
*将使用值在指定路径处向树中添加项
*等于路径中的最后一项,除非该项已存在
*/
公共void addData(列表路径)
{
addData(这个,路径);
}
私有void addData(树项父项,列表路径)
{
//如果我们在路径的末尾-创建一个节点
字符串数据=path.get(0);
if(path.size()==1)
{
//除非已经存在具有此名称的节点
如果(!parent.hasChild(数据))
{
组=新组();
group.setName(数据);
parent.addChild(数据,组);
}
}
其他的
{
//将此路径的尾部向下传递到层次结构中的下一个级别
addData(parent.getChild(data),path.subList(1,path.size());
}
}
公共组getData(字符串[]路径)
{
return(Group)getData(this,Arrays.asList(path));
}
公共组getData(列表路径)
{
返回(组)getData(此,路径);
}
私有TreeItem getData(TreeItem父级,列表路径)
{
如果(pa
{"data":[{"id":1,"children":
[{"id": 2,"children":
[{"id": 3 ,"children":
[{"id": 4,"name":"Prius"}],"name":"Compact"}],"name":"Hybrid"},
{"id":5,"children":
[{"id":3,"children":
[{"id":7,"MPG":38, "name":"Corolla"},
{"id":8,"MPG":28,"name":"Camry"}],"name":"Compact"}],"name":"Gas"}],"name":"Toyota"},
{"id":9, "children":
[{"id":10,"children":
[{"id":3 ,"children":
[{"id":11 ,"name":"Civic"}],"name":"Compact"}],"name":"Gas"}],"name":"Honda"}]}
public class Test {
public static void main(String[] args)
{
// hierarchical data in a flattened list
String[][] data = {
{"Toyota", "Gas", "Compact", "Corolla"},
{"Toyota", "Gas", "Compact", "Camry"},
{"Toyota", "Hybrid", "Compact", "Prius"},
{"Honda", "Gas", "Compact", "Civic"}
};
TreeManager treeManager = new TreeManager();
for(String[] row : data)
{
// build the path to our items in the tree
List<String> path = new ArrayList<String>();
for(String item : row)
{
// add this item to our path
path.add(item);
// will add it unless an Item with this name already exists at this path
treeManager.addData(treeManager, path);
}
}
treeManager.getData(data[0]).putValue("MPG", 38);
treeManager.getData(data[1]).putValue("MPG", 28);
Gson gson = new Gson();
System.out.println(gson.toJson(treeManager));
}
/**
* This base class provides the hierarchical property of
* an object that contains a Map of child objects of the same type.
* It also has a field - Name
*
*/
public static abstract class TreeItem implements Iterable<TreeItem>{
private Map<String, TreeItem> children;
private String name;
public TreeItem() {
children = new HashMap<String, TreeItem>();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addChild(String key, TreeItem data)
{
children.put(key, data);
}
public TreeItem getChild(String key)
{
return children.get(key);
}
public boolean hasChild(String key)
{
return children.containsKey(key);
}
@Override
public Iterator<TreeItem> iterator() {
return children.values().iterator();
}
}
/**
* This is our special case, root node. It is a TreeItem in itself
* but contains methods for building and retrieving items from our tree
*
*/
public static class TreeManager extends TreeItem
{
/**
* Will add an Item to the tree at the specified path with the value
* equal to the last item in the path, unless that Item already exists
*/
public void addData(List<String> path)
{
addData(this, path);
}
private void addData(TreeItem parent, List<String> path)
{
// if we're at the end of the path - create a node
String data = path.get(0);
if(path.size() == 1)
{
// unless there is already a node with this name
if(!parent.hasChild(data))
{
Group group = new Group();
group.setName(data);
parent.addChild(data, group);
}
}
else
{
// pass the tail of this path down to the next level in the hierarchy
addData(parent.getChild(data), path.subList(1, path.size()));
}
}
public Group getData(String[] path)
{
return (Group) getData(this, Arrays.asList(path));
}
public Group getData(List<String> path)
{
return (Group) getData(this, path);
}
private TreeItem getData(TreeItem parent, List<String> path)
{
if(parent == null || path.size() == 0)
{
throw new IllegalArgumentException("Invalid path specified in getData, remainder: "
+ Arrays.toString(path.toArray()));
}
String data = path.get(0);
if(path.size() == 1)
{
return parent.getChild(data);
}
else
{
// pass the tail of this path down to the next level in the hierarchy
return getData(parent.getChild(data), path.subList(1, path.size()));
}
}
}
public static class Group extends TreeItem {
private Map<String, Object> properties;
public Object getValue(Object key) {
return properties.get(key);
}
public Object putValue(String key, Object value) {
return properties.put(key, value);
}
public Group () {
super();
properties = new HashMap<String, Object>();
}
}
}