-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformJson.js
70 lines (66 loc) · 2.74 KB
/
formJson.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
var generateRes = function(str) {
try {
var json = JSON.parse(str);
if(typeof(json) == "object")
return formatRes(null, json, 0, false, true);
else
return str;
} catch (err) {
return str;
}
}
var formatRes = function(key, obj, depth, isArray, isEnd) {
var margin = 20;
var els = "",
mlValue = margin * (depth + 1);
var jsonType = Object.prototype.toString.call(obj);
//区分数组和json对象
if("[object Array]" == jsonType) { //解析数组
els += "<p style='margin-left: " + (mlValue - margin) + "px;'>" + (key ? (key + ":") : "") + " [</p>";
$.each(obj, function(index, element) {
var type = Object.prototype.toString.call(element),
isLast = index == (obj.length - 1),
value = element;
type = type.match(/\[object (\S*)\]/)[1];
//基本数据类型
if("Boolean" == type || "Number" == type || "Null" == type)
els += "<p style='margin-left: " + mlValue + "px;'>" + jsonkey + ": <span class='json-" + type + "'>" + value
+ "</span>" + (isLast ? "" : ",") + "</p>";
else if("String" == type) {
value = value.replace(new RegExp(/</g),'\\u003c').replace(new RegExp(/>/g),'\\u003e')
els += "<p style='margin-left: " + mlValue + "px;'>" + jsonkey + ": <span class='json-" + type + "'>\"" + value
+ "\"</span>" + (isLast ? "" : ",") + "</p>";
} else
els += formatRes(null, element, depth + 1, true, isLast);
});
els += "<p style='margin-left: " + (mlValue - margin) + "px;'>]" + (isEnd ? "" : ",") + "</p>";
} else { //解析json
els += "<p style='margin-left: " + (mlValue - margin) + "px;'>" + (key ? (key + ":") : "") + " {</p>";
var length = 0;
for(var jsonkey in obj) {
length++;
}
var index = 0;
for(var jsonkey in obj) {
index++;
var value = obj[jsonkey],
isLast = index == length;
var type = Object.prototype.toString.call(value),
el;
type = type.match(/\[object (\S*)\]/)[1];
//基本数据类型
if("Boolean" == type || "Number" == type || "Null" == type)
el= "<p style='margin-left: " + mlValue + "px;'>" + jsonkey + ": <span class='json-" + type + "'>" + value + "</span>" + (isLast ? "": ",") + "</p>";
else if("String" == type) {
value = value.replace(new RegExp(/</g),'\\u003c').replace(new RegExp(/>/g),'\\u003e')
el= "<p style='margin-left: " + mlValue + "px;'>" + jsonkey + ": <span class='json-" + type + "'>\"" + value + "\"</span>" + (isLast ? "": ",") + "</p>";
} else if("Array" == type)
el = formatRes(jsonkey, value, depth + 1, true, isLast);
else
el = formatRes(jsonkey, value, depth + 1, false, isLast);
els += el;
}
els += "<p style='margin-left: " + (mlValue - margin) + "px;'>}" + (isArray && !isEnd ? ",": "") + "</p>";
}
return els;
}