-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonstringify.js
More file actions
65 lines (59 loc) · 1.71 KB
/
jsonstringify.js
File metadata and controls
65 lines (59 loc) · 1.71 KB
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
// json stringify function initialization
var jsonStringify = function (object) {
if (object === null) {
return null;
}
if (typeof object === 'string') {
return "\"" + object + "\""
}
if (typeof object !== 'object') {
return object;
}
if (Array.isArray(object)) {
let res = ""
res += '['
for (let i = 0; i < object.length; i++) {
if (i === object.length - 1) {
res += jsonStringify(object[i]) + ''
} else {
res += jsonStringify(object[i]) + ','
}
}
res += ']'
return res;
}
if (typeof object === 'object') {
let re = ''
re += '{';
for (var key in object) {
if (object.hasOwnProperty(key)) {
re += "\"" + key + "\":"
re += jsonStringify(object[key]) + ','
}
}
if (re.endsWith(',')) {
re = re.substring(0, re.length - 1);
}
re += '}';
return re;
}
};
//objects
let obj = { "y": 1, "x": 2 };
let obj2 = { "a": "str", "b": -12, "c": true, "d": null };
let obj3 = { "key": { "a": 1, "b": [{}, null, "Hello"] } };
let obj4 = true;
let obj5 = [1, 2, 3, 'shdf']
let obj6 = [1, 2, 3, 'shdf', [5, 6, "hi", [5, 6, "hi"]]]
let obj7 = [1, 2, 3, null, [5, 6, "hi", [5, 6, "hi"]]]
let obj8 = [{}, null, "Hello"];
//function call
console.log(jsonStringify(obj));
console.log(jsonStringify(obj2));
console.log(jsonStringify(obj3));
console.log(jsonStringify(obj4));
console.log(jsonStringify(obj5));
console.log(jsonStringify(obj6));
console.log(jsonStringify(obj7));
console.log(jsonStringify(obj8));
// console.log(typeof jsonStringify(obj));