首先,您提供的 JSON 不是有效的 JSON。这样我在生成 JSON 时添加了一个父根节点。
如果您在数据集中定义了一个根节点,那么您的结构将会发生微小的变化,因为您无法将 parent_Id = child_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 );
}
}
对于您的原始数据类型,我创建了 Mapping.java
public class Mapping
{
private int parentId;
private int childId;
private String ItemName;
public Mapping( int parentId, int childId, String itemName )
{
this.parentId = parentId;
this.childId = childId;
ItemName = itemName;
}
public int getParentId()
{
return parentId;
}
public void setParentId( int parentId )
{
this.parentId = parentId;
}
public int getChildId()
{
return childId;
}
public void setChildId( int childId )
{
this.childId = childId;
}
public String getItemName()
{
return ItemName;
}
public void setItemName( String itemName )
{
ItemName = itemName;
}
}
现在加载和数据并填充节点对象并获得最终的 Json 在这里完成。我在这里使用对象引用映射,因为我们无法保证您的数据库中映射的顺序。由于children被赋值给parent的对象引用,完成赋值后,我们就有了我们的父子结构。
出于同样的原因,也使用了这两个循环。在开始构建结构之前,我们需要确保 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。
生成的 JSON
{
"root":[
{
"Country":[
{
"Australia":[
"Victoria",
"Queensland"
]
},
{
"Canada":[
{
"British Columbia":[
"Vancouver"
]
}
]
}
]
},
{
"Songs":[
"Song1",
{
"Song2":[
"lyrics 1st",
"lyrics 2nd"
]
}
]
},
"Germany",
{
"England":[
"London"
]
}
]
}
希望对你有帮助。