在java中将树结构从数据库转换为JSON对象?
我在数据库中有一个父子关系(树结构),我想遍历它并从中创建一个json对象 我的数据库父子关系结构(演示数据) 这是如何工作的在java中将树结构从数据库转换为JSON对象?,java,json,recursion,tree,tree-traversal,Java,Json,Recursion,Tree,Tree Traversal,我在数据库中有一个父子关系(树结构),我想遍历它并从中创建一个json对象 我的数据库父子关系结构(演示数据) 这是如何工作的 if(child_id == parent_id) { // This item is parent. Like Country,Songs,Germany, England } else { // This item is child. Like Australia, Song1, Vancouver etc. } { "country
if(child_id == parent_id)
{
// This item is parent. Like Country,Songs,Germany, England
} else {
// This item is child. Like Australia, Song1, Vancouver etc.
}
{
"country": [
{
"Australia": [
"Victoria",
"Queensland"
]
},
{
"Canada": [
{
"British Columbia": [
"Vancouver"
]
}
]
}
]
},
{
"Songs": [
"Songs1",
{
"Songs2": [
"lyrics 1st",
"lyrics 2nd"
]
}
]
},
{
"Germany": null
},
{
"England": ["London"]
}
现在,我知道如何遍历这样的结构,但在将其转换为json对象时遇到了困难
伪代码DFS树
得到所有家长的
List data = fetch data from table where parent_id=child_id
现在遍历此数据
Recursively iterate through child elements
get child_id from data object and query on database it as parent_id, to get its child elements and so on.
但是,如何将其转换为类似以下的JSON对象
if(child_id == parent_id)
{
// This item is parent. Like Country,Songs,Germany, England
} else {
// This item is child. Like Australia, Song1, Vancouver etc.
}
{
"country": [
{
"Australia": [
"Victoria",
"Queensland"
]
},
{
"Canada": [
{
"British Columbia": [
"Vancouver"
]
}
]
}
]
},
{
"Songs": [
"Songs1",
{
"Songs2": [
"lyrics 1st",
"lyrics 2nd"
]
}
]
},
{
"Germany": null
},
{
"England": ["London"]
}
或者,在其中维护父子关系的json对象首先,您提供的json不是有效的json。因此,我在生成JSON时添加了一个父根节点 如果在数据集中定义了根节点,则结构会有小的更改,因为无法将父\u Id=子\u Id关系作为当前数据集来维护。因此,解决方案也会有修改 首先,您需要将数据映射到某种父子支持的数据类型。 为此,我创建了Node.java。介绍了
addChild
方法逐个添加子项
import java.util.ArrayList;
import java.util.List;
public class Node
{
private String nodeName;
private java.util.List<Node> children = new ArrayList<Node>();
public Node( String nodeName )
{
this.nodeName = nodeName;
}
public List<Node> getChildren()
{
return children;
}
public void setChildren( List<Node> children )
{
this.children = children;
}
public String getNodeName()
{
return nodeName;
}
public void setNodeName( String nodeName )
{
this.nodeName = nodeName;
}
public void addChild( Node node )
{
this.children.add( node );
}
}
现在,这里完成了加载和数据、填充节点对象以及获取最终Json。我在这里使用对象引用映射,因为我们无法保证数据库中映射的顺序。由于子对象被指定给父对象的对象引用,因此在完成指定后,我们将拥有父子结构。
这两个循环也是出于同样的原因使用的。在开始构建结构之前,我们需要确保map拥有所有节点
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ParentChild
{
public static void main( String[] args )
{
List<Mapping> list = new ArrayList<Mapping>();
list.add( new Mapping( 1, 1, "Country" ) );
list.add( new Mapping( 1, 2, "Australia" ) );
list.add( new Mapping( 2, 3, "Victoria" ) );
list.add( new Mapping( 2, 4, "Queensland" ) );
list.add( new Mapping( 1, 5, "Canada" ) );
list.add( new Mapping( 5, 6, "British Columbia" ) );
list.add( new Mapping( 6, 7, "Vancouver" ) );
list.add( new Mapping( 8, 8, "Songs" ) );
list.add( new Mapping( 8, 9, "Song1" ) );
list.add( new Mapping( 8, 10, "Song2" ) );
list.add( new Mapping( 10, 11, "lyrics 1st" ) );
list.add( new Mapping( 10, 12, "lyrics 2nd" ) );
list.add( new Mapping( 13, 13, "Germany" ) );
list.add( new Mapping( 14, 14, "England" ) );
list.add( new Mapping( 14, 15, "London" ) );
Map<Integer, Node> map = new HashMap<Integer, Node>();
map.put( -1, new Node( "root" ) ); // give index -1 for the root
for( Mapping mapping : list ) // keep a map of nodes by child id
{
map.put( mapping.getChildId(), new Node( mapping.getItemName() ) );
}
for( Mapping mapping : list )
{
if( mapping.getParentId() == mapping.getChildId() )
{
map.get( -1 ).addChild( map.get( mapping.getChildId() ) ); // add to the root
}
else
{
Node node = map.get( mapping.getParentId() );
Node childNode = map.get( mapping.getChildId() );
node.addChild( childNode ); // add to the relevant parent
}
}
StringBuilder json = new StringBuilder();
writeJson( map.get( -1 ), json ); // root node is enough
System.out.println( json );
}
private static void writeJson( Node node, StringBuilder json )
{
if( node.getChildren().isEmpty() ) // no children. return just the node name
{
json.append( "\"" ).append( node.getNodeName() ).append( "\"" );
}
else
{
json.append( "{\"" ).append( node.getNodeName() ).append( "\": [" );
List<Node> children = node.getChildren();
for( int i = 0; i < children.size(); i++ )
{
Node child = children.get( i );
writeJson( child, json ); // call recursively
if( i != children.size() - 1 ) // skip , for the last child
{
json.append( "," );
}
}
json.append( "]}" );
}
}
}
希望有帮助。实际上,我提供的json忘记了在对象之间添加逗号,现在我已经修改了它。“国家”、“歌曲”、“德国”和“英国”。都是不同的对象。@John您的json仍然无效。无论如何,既然您需要“一个维护父子关系的json对象”,希望这个答案对您有所帮助。
{
"root":[
{
"Country":[
{
"Australia":[
"Victoria",
"Queensland"
]
},
{
"Canada":[
{
"British Columbia":[
"Vancouver"
]
}
]
}
]
},
{
"Songs":[
"Song1",
{
"Song2":[
"lyrics 1st",
"lyrics 2nd"
]
}
]
},
"Germany",
{
"England":[
"London"
]
}
]
}