I felt confused trying to parse a Json String into corresponding Json token, what I have sorted out currently still gets stuck at basic steps.
So, it is import to read very basic data types that are values, including numeric values, boolean, and string.
Reading other types of basic values from a string is easy, comparing to reading string. The mind teasing thing about reading a string from a string is the escape character, I figured when read character is '\\', because \ is a special character as escape, single \ is already judged by compiler as wrong gramma, so, it is basically \\ for \ in a string.
In above case, the dest string that I want should append a character, is the previous \\ for escaping? I will should have to read one more character, and judge if it is for escaping, chances are: I get a \", so I should append \" to my dest string; I get a \\, I should append \\ to my dest string, \u+4digits, I should append a unicode char translated from the code sequence to my dest string; So, in general, they should be the 3 chances to get after a \\ is first read from a string.
Now I should write more basic 3 methods of reading them: i.e. readEscapedUnicode, readEscapedBackSlash,readEscapedSingleDoubleQuotationMark, then append them to the dest string.
private String readStringFromString() throws IOException {
String ret = "";
int count = 0;
while (true) {
counter++;
char cur = (char) stringReader.read();
if (cur == '\"') {
count++;
if (count == 2) {//结束条件才应该返回,判断好结束条件
return ret;
}
continue;
}
if (cur == '\\') {//need escape,next char should append to result as plain char,转义的要点就在于下一个字符要不要加入结果,这个其实是一个\,不然这个\是要去掉不显示的
char next = (char) stringReader.read();
counter++;
if (next == '\\') {
ret += next; //ret+=\, the only way to append \ as escape char,
} else
ret = ret + cur + next;//不是特殊的,直接合并
}
ret += cur;
}
}
For
"\"a34b\\\\\\\"bsdf\"" ,it reads: 'a34b\\\"\bsdf' as a string content; when wrapped in "", it is: "a34b\\\"\bsdf", this is the result that is correct; \ is one char, and \" is also one char; the result is a wrapped in "" by default, \\ \" is \" when translated, this executes
escape for once. only \" is special, the escape before it has to be a escaped char, so, here has 2 escapes, and when comparing
if (next == '\\') {
ret += next;
ret is only append one \, this part is a bit mind teasing, same for
String a="";
a+="\"";
a is only " in the inside;
Reading content is like below:
@Test
public void test1() throws IOException {
String json = "{ \"deleteFlag\":23,\"codeId\":[\"1\",2,4],\"codeName\":\"IT\",\"sort\":\"1\",\"id\":\"60\"}";
StringReader sr = new StringReader(json);
ArrayList<String> words = new ArrayList<>();
String curW = "";
while (true) {
int cur = sr.read();
if (cur == -1) break;
if ((char) cur == ',') {
words.add(",");
}
if ((char) cur == '{') {
words.add("{");
} else if ((char) cur == '}') {
words.add("}");
} else if ((char) cur == ':') {
words.add(":");
} else if ((char) cur == '[') {
words.add("[");
} else if ((char) cur == ']') {
words.add("]");
} else if (cur != ' ' && cur != '\"' && cur != ',') {
String w = "" + (char) cur;
while (true) {
int cur1 = sr.read();
if (cur1 == -1) break;
if (cur1 == ',' || cur1 == '}' || cur1 == ']') {
words.add(w);
words.add("" + (char) cur1);
break;
}
w += (char) cur1;
}
} else if (cur == '\"') {
String w = "";
while (true) {
int cur1 = sr.read();
if (cur1 == -1) break;
if (cur1 != '\"') {
w += (char) cur1;
} else if (cur1 == '\"') {
words.add(w);
break;
}
}
} else if ((char) cur == ' ') {
continue;
}
}
for (String w : words) {
System.out.println(w);
}
}