海露-XShell.CN

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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* @author hailou
* @Description
* @Date on 2020/9/18 16:30
*/

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Json implements Comparable<Json> {
// There are value types
private int Type_String = 1;
private int Type_Array = 2;
private int Type_List = 3;
// Key always is String
private String key;
// There are three types of value
private int valueType;
private String valueString;
private List<Json> valueArray;// 本质一致,表现不同
private List<Json> valueList;
// indent depth
private int depth;

public Json(String key, String value) {
this.key = key;
this.valueType = Type_String;
this.valueString = value;
this.depth = 0;
}
public Json(String key, int type) {
this.key = key;

if (type == Type_List) {
this.valueType = Type_List;
this.valueList = new LinkedList<Json>();
} else if (type == Type_Array) {
this.valueType = Type_Array;
this.valueArray = new LinkedList<Json>();
}
}

public void addJsonToList(Json json) {
if (valueList != null) {
valueList.add(json);

adjustDepth();
}
}

public void addJsonToArray(Json json) {
if (valueArray != null) {
valueArray.add(json);

adjustDepth();
}
}

private void adjustDepth() {
if (valueType == Type_List) {
for (Json json : valueList) {
json.depth = this.depth + 1;
json.adjustDepth();
}
}

if (valueType == Type_Array) {
for (Json json : valueArray) {
json.depth = this.depth + 1;
json.adjustDepth();
}
}
}

public String toString() {
StringBuilder sb = new StringBuilder();
// key
String tabs = getIndentSpace();
sb.append(tabs);
//sb.append("\""+(key==null?"":key)+"\"");
if (key != null) {
sb.append("\"" + key + "\"");
sb.append(":");
} else {

}
// value
if (valueType == Type_String) {
sb.append("\"" + valueString + "\"");
} else if (valueType == Type_Array) {
sb.append("[\n");
int n = valueArray.size();
for (int i = 0; i < n; i++) {
Json json = valueArray.get(i);
if (i != n - 1) {
sb.append(json.toString() + ",\n");
} else {
sb.append(json.toString() + "\n");
}
}
sb.append(tabs + "]");
} else if (valueType == Type_List) {

sb.append("{\n");
Collections.sort(valueList);
int n = valueList.size();

for (int i = 0; i < n; i++) {
Json json = valueList.get(i);
if (i != n - 1) {
sb.append(json.toString() + ",\n");
} else {
sb.append(json.toString() + "\n");
}
}
sb.append(tabs + "}");
}
return sb.toString();
}

public int compareTo(Json other) {
return this.key.compareTo(other.key);
}

private String getIndentSpace() {
return String.join("", Collections.nCopies(this.depth, " "));
}

public static void main(String[] args) {
Json id1 = new Json("id", "001");
Json name1 = new Json("name", "白菜");

Json title = new Json("title", 3);
title.addJsonToList(id1);
title.addJsonToList(name1);

Json empty1 = new Json(null, 3);
empty1.addJsonToList(new Json("id", "001"));
empty1.addJsonToList(new Json("id", "你好白菜"));

Json empty2 = new Json(null, 3);
empty2.addJsonToList(new Json("id", "001"));
empty2.addJsonToList(new Json("id", "你好萝卜"));

Json content = new Json("content", 2);
content.addJsonToArray(empty1);
content.addJsonToArray(empty2);

Json data = new Json("data", 3);
data.addJsonToList(title);
data.addJsonToList(content);

Json status = new Json("status", "0000");
Json message = new Json("message", "success");

Json root = new Json(null, 3);
root.addJsonToList(status);
root.addJsonToList(message);
root.addJsonToList(data);

System.out.println(root.toString());
}
}

如果对您有帮助,请作者喝杯LK:

 评论


博客内容遵循 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 协议

本站使用 Material X 作为主题 , 总访问量为 次 。
字数统计:17.9k 载入天数...载入时分秒...