package foundation.json.tree; import foundation.json.JSONBuilder; import foundation.json.JSONReader; import foundation.json.JType; public class JTree extends JNode { public JTree() { super("Root", "Root", JType.Root, 0); } public void loadJSON(JSONReader rootReader) { if (rootReader == null || rootReader.isEmpty()) { return; } JNode node = null; String fullName, name; for (JSONReader reader: rootReader) { name = reader.getName(); fullName = name; node = JNode.newInstance(reader.getType(), fullName, name, level); if (!reader.hasChildren()) { continue; } node.loadJSON(reader, level + 1); } } public void toJSON(JSONBuilder builder) { for (JNode node: children) { node.toJSON(builder); } } public void loadBand(JBand band) { if (band.isEmpty()) { return; } JBandItem item = band.next(); loadBandItem(band, item); } @Override public void toBand(JBand band) { for (JNode node: children) { node.toBand(band); } } @Override protected void loadBandItem(JBand band, JBandItem item) { int itemLevel = 1; //1. 加载当前 Item JNode node = JNode.createInstance(this, item.getName(itemLevel), item.getType(itemLevel), itemLevel); node.loadBandItem(band, item); //2. 尝试加载后面的 Item while (band.hasNext(name, level)) { item = band.next(); node = JNode.createInstance(this, item.getName(itemLevel), item.getType(itemLevel), itemLevel); node.loadBandItem(band, item); } } public static void main(String[] args) { JBand band = new JBand(); band.appendOne("result.is_success", "true"); band.appendOne("result.data.org.code", "001"); band.appendOne("result.data.org.name", "荣光公司"); band.appendOne("result.data.address[0].name", "办公地址"); band.appendOne("result.data.address[0].location", "大兴区11号"); band.appendOne("result.data.address[1].name", "家庭地址"); band.appendOne("result.data.address[1].location", "大兴区22号"); band.appendOne("result.data.contact.code", "C01"); band.appendOne("result.data.contact.name", "赵容"); JTree tree = new JTree(); tree.loadBand(band); band = new JBand(); tree.toBand(band); System.out.println(band); } }