forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-json-string-to-object.ts
70 lines (69 loc) · 2.12 KB
/
convert-json-string-to-object.ts
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
// Time: O(n)
// Space: O(h)
// stack
function jsonParse(str: string): any {
let stk = [];
let result = null;
let k = null;
for (let i = 0; i < str.length; i++) {
if (str[i] === "," || str[i] === ":") {
continue;
}
if (str[i] === '{' || str[i] === '[') {
let v = str[i] === '{' ? {} : [];
if (result !== null) {
if (k !== null) {
result[k] = v;
k = null;
} else {
result.push(v);
}
stk.push(result);
}
result = v;
} else if (str[i] === '}' || str[i] === ']') {
if (stk.length !== 0) {
result = stk.pop();
}
} else {
let v;
if (str[i] === '-' || ('0' <= str[i] && str[i] <= '9')) {
let j = i;
while (i + 1 < str.length &&
(str[i + 1] === '-' || ('0' <= str[i + 1] && str[i + 1] <= '9') || str[i + 1] === '.')) {
i++;
}
v = Number(str.substring(j, i + 1));
} else if (str[i] === '"') {
let j = i + 1;
while (i + 1 < str.length && str[i + 1] !== '"') {
i++;
}
v = str.substring(j, i + 1);
i++;
} else {
if (i + 3 < str.length && str.substring(i, i + 4) === "true") {
v = true;
i += 3;
} else if (i + 4 < str.length && str.substring(i, i + 5) === "false") {
v = false;
i += 4;
} else {
v = null;
i += 3;
}
}
if (result === null) {
result = v;
} else if (str[i + 1] === ":") {
k = v;
} else if (k !== null) {
result[k] = v;
k = null;
} else {
result.push(v);
}
}
}
return result;
};