JS手动实现解析JSON

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
console.log(parseText(`
{
"transformRequest":
{},
"transformResponse":
{},
"timeout": 10000,
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": ["X-XSRF-TOKEN","X-XSRF-TOKEN2","X-XSRF-TOKEN3"],
"maxContentLength": -1,
"headers":
{
"Accept": "application/json, text/plain, */*",
"Content-Type": "application/json;charset=UTF-8"
},
"baseURL": "/test",
"method": "post",
"url": "/test",
"data": "{\\"page\\":1,\\"limit\\":10}"
}`));

实现

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
let tokenReg = {
string: new RegExp("^(\"([^\"\\\\]|\\\\.)*\")"),
number: new RegExp("^(-?\\d+(\\.\\d+)?)"),
boolean: new RegExp("^(true|false)"),
null: new RegExp("^(null)"),
objectOpen: new RegExp("^(\\{)"),
objectClose: new RegExp("^(\\})"),
arrayOpen: new RegExp("^(\\[)"),
arrayClose: new RegExp("^(\\])"),
colon: new RegExp("^(\\:)"),
split: new RegExp("^(\\,)"),
space: new RegExp("^(\\s+)"),
}

function getFirstToken(text, type) {
if (!tokenReg[type].test(text)) {
return null;
}
return {
type,
match: text.match(tokenReg[type])[1]
};
}

function transString(text) {
let val = "";
for (let i = 1; i < text.length - 1; i++) {
if (text[i] != "\\") {
val += text[i];
} else {
switch (text[i + 1]) {
case "n":
val += "\n";
break;
case "t":
val += "\t";
break;
default:
val += text[i + 1];
break;
}
i++;
}
}
return val;
}

function parseTokens(tokens, index) {
switch (tokens[index].type) {
case "objectOpen": {
index++;
let obj = {};
while (tokens[index].type != "objectClose") {
if (tokens[index].type == "string" && tokens[index + 1].type == "colon") {
let item = parseTokens(tokens, index + 2);
obj[transString(tokens[index].match)] = item.data;
index = item.nextIndex;
if (tokens[index].type == 'split') {
index++;
}
} else {
throw new Error();
}
}
return {
nextIndex: index + 1,
data: obj
};
}
case "arrayOpen": {
index++;
let obj = [];
while (tokens[index].type != "arrayClose") {
let item = parseTokens(tokens, index);
obj.push(item.data);
index = item.nextIndex;
if (tokens[index].type == 'split') {
index++;
}
}
return {
nextIndex: index + 1,
data: obj
};

}
case "string": {
return {
nextIndex: index + 1,
data: transString(tokens[index].match)
};
}
case "number": {
return {
nextIndex: index + 1,
data: Number(tokens[index].match)
};
}
case "boolean": {
return {
nextIndex: index + 1,
data: Boolean(tokens[index].match)
};
}
case "null": {
return {
nextIndex: index + 1,
data: null
};
}
default:
throw new Error();
}
}

function formatTokens(tokens) {
let format = "";
let deepth = 0;
const enter = (dp) => format += "\n" + " ".repeat(dp);
for (let item of tokens) {
if (item.type == 'objectClose' || item.type == 'arrayClose') {
enter(--deepth);
}
format += item.match;
if (item.type == 'objectOpen' || item.type == 'arrayOpen') {
enter(++deepth);
}
if (item.type == 'split') {
enter(deepth);
}
if (item.type == 'value') {
format += " ";
}
}
return format;
}

function parseText(text) {
let tokens = [];
while (text) {
let item
for (let k in tokenReg) {
item = getFirstToken(text, k);
if (item) {
break;
}
}
tokens.push(item);
text = text.substring(item.match.length);
}
tokens = tokens.filter(o => o.type != "space");
console.log(formatTokens(tokens));
return parseTokens(tokens, 0).data;
}
 欢迎关注我的博客公众号
如果觉得我的文章对你有用,请随意赞赏
0%