P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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);
    }
 
}