📄 jsonutils.java
字号:
boolean isValue = false;
//
int index = start;
int len = data.length();
//
String key = "";
String value = "";
while (index < len) {
char c = data.charAt(index);
//System.out.println(c);
if (c == '{') {
if (jsonObject == null) {
jsonObject = new JsonObject(inputDatePattern);
// jsonObject begin
isKey = true;
isValue = false;
} else {
int end = cutJsonObject(data, index);
value = data.substring(index, end);
index = end - 1;
isKey = false;
isValue = true;
//System.out.println("end : " + end);
//System.out.println("value : " + value);
}
//
} else if (c == '}') {
// jsonObject end
// value end
isValue = false;
isKey = true;
char cc = value.trim().charAt(0);
if (cc == '{') {
jsonObject.put(key, parseObject(value, 0));
} else if (cc == '[') {
jsonObject.put(key, parseArray(value, 0));
} else {
jsonObject.put(key, parseValue(value, 0));
}
key = "";
value = "";
break;
} else if (c == '[') {
int end = cutJsonArray(data, index);
value = data.substring(0, end);
index = end - 1;
isKey = false;
isValue = true;
//
} else if ((c == '"') || (c == '\'')) {
int end = cutJsonString(data, index);
if (isKey) {
key = data.substring(index, end);
} else {
value = data.substring(index, end);
}
index = end - 1;
// string begin or end
} else if (c == ':') {
// key end, value start
isKey = false;
isValue = true;
} else if (c == ',') {
// value end
isValue = false;
isKey = true;
char cc = value.trim().charAt(0);
if (cc == '{') {
jsonObject.put(key, parseObject(value, 0));
} else if (cc == '[') {
jsonObject.put(key, parseArray(value, 0));
}
jsonObject.put(key, parseValue(value, 0));
//System.out.println("key: " + key + ", value: " + value);
//System.out.println(jsonObject);
key = "";
value = "";
} else {
if (isKey) {
key += c;
} else if (isValue) {
value += c;
}
}
index++;
}
return jsonObject;
}
public JsonArray parseArray(String data, int start) {
//
JsonArray jsonArray = null;
//
int index = start;
int len = data.length();
//
String value = "";
while (index < len) {
char c = data.charAt(index);
//System.out.println(c);
if (c == '{') {
int end = cutJsonObject(data, index);
value = data.substring(index, end);
index = end - 1;
//
//System.out.println("jsonObject begin: " + objectCount);
} else if (c == '[') {
if (jsonArray == null) {
jsonArray = new JsonArray();
} else {
int end = cutJsonArray(data, index);
value = data.substring(index, end);
index = end - 1;
}
// jsonArray begin
//System.out.println("jsonArray begin: " + arrayCount);
} else if ((c == '\'') || (c == '"')) {
int end = cutJsonString(data, index);
value = data.substring(index, end);
index = end - 1;
} else if (c == ']') {
// jsonArray end
value = value.trim();
//System.out.println(value);
//System.out.println(jsonArray);
if (value.length() > 0) {
// value end
char cc = value.charAt(0);
if (cc == '[') {
jsonArray.add(parseArray(value, 0));
} else if (cc == '{') {
jsonArray.add(parseObject(value, 0));
} else {
jsonArray.add(parseValue(value, 0));
}
//System.out.println(jsonArray);
value = "";
}
} else if (c == ',') {
// value end
char cc = value.trim().charAt(0);
if (cc == '[') {
jsonArray.add(parseArray(value, 0));
} else if (cc == '{') {
jsonArray.add(parseObject(value, 0));
} else {
jsonArray.add(parseValue(value, 0));
}
//System.out.println("key: " + key + ", value: " + value);
value = "";
} else if (c == ' ') {
// blank
value += ' ';
} else {
value += c;
}
index++;
}
return jsonArray;
}
public JsonValue parseValue(String data, int start) {
String value = data.trim();
int len = value.length();
char c = value.charAt(0);
if ((c == '\'') || (c == '"')) {
value = value.substring(1, len - 1);
return new JsonValue(value, JsonValue.TYPE_STRING);
} else {
return new JsonValue(value);
}
}
public static int cutJsonObject(String data, int start) {
int len = data.length();
int index = start;
char c;
int num = 0;
while (index < len) {
c = data.charAt(index);
if (c == '{') {
num++;
} else if (c == '}') {
num--;
}
index++;
if (num == 0) {
break;
}
}
return index;
}
public static int cutJsonArray(String data, int start) {
int len = data.length();
int index = start;
char c;
int num = 0;
while (index < len) {
c = data.charAt(index);
if (c == '[') {
num++;
} else if (c == ']') {
num--;
}
index++;
if (num == 0) {
break;
}
}
return index;
}
public static int cutJsonString(String data, int start) {
boolean isChange = false;
boolean isSingleQuote = true;
if ('"' == data.charAt(start)) {
isSingleQuote = false;
} else {
isSingleQuote = true;
}
int len = data.length();
int index = start + 1;
char c;
while (index < len) {
c = data.charAt(index);
if (isChange) {
isChange = false;
} else {
if (c == '\\') {
isChange = true;
} else if (isSingleQuote && (c == '\'')) {
break;
} else if (!isSingleQuote && (c == '"')) {
break;
}
}
index++;
}
return index + 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -