-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirsthm.js
99 lines (86 loc) · 2.17 KB
/
firsthm.js
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
var user = {
id: 7,
name: {
firstName: "Nikolay",
lastName: "Okunev"
},
email: [
"first@gmail.com",
"second@gmail.com"
],
additional: {
x: 6,
y: 8,
deep: {
x: "valuez"
}
}
}
function println(user) {
document.getElementById("inputfield").textContent = user;
}
function isObject(val) {
return val instanceof Object;
}
function isArray(object) {
if (object.constructor === Array) return true;
else return false;
}
function isNumeric(num){
return !isNaN(num)
}
println(jsonToString(user));
//println(JSON.stringify(user));
function jsonToString(yourJson) {
var result = "{";
result = cicle(result, yourJson);
result = result + "}";
return result;
}
function cicle(result, node) {
if (isObject(node)) {
if (isArray(node)) {
result = result + "[" + arrayToNiceString(node) + "]";
} else {
var childNodesList = getChildNodesAsList(node);
var counter = 0;
while (counter < childNodesList.length) {
var newJson = node[childNodesList[counter]];
if (counter>0){
result = result +",";
}
result = result + "\"" + childNodesList[counter] + "\":";
if (isObject(newJson) && !isArray(newJson)) {
result = result + "{"
}
result = cicle(result, newJson);
counter++;
if (isObject(newJson) && !isArray(newJson)) {
result = result + "}";
}
}
}
} else {
if (isNumeric(node)){
result = result + node;
}else{
result = result + "\"" +node+"\"";
}
}
return result;
}
function arrayToNiceString(arrayBefore){
var result="";
var counter=0;
while(counter<arrayBefore.length){
if (counter>0){
result=result+",";
}
result=result+"\""+arrayBefore[counter]+"\"";
counter++;
}
return result;
}
function getChildNodesAsList(yourJson) {
return Object.keys(yourJson);
}