-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
81 lines (74 loc) · 2.14 KB
/
index.html
File metadata and controls
81 lines (74 loc) · 2.14 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>my-first-website</title>
<LINK href="styles.css" rel="stylesheet" type="text/css">
<script>
function jsontolua(jsonObject, tabs = "") {
var contentTabs = tabs + " ";
var result = "null";
if (jsonObject != null) {
if (Array.isArray(jsonObject))
{
var arrayLen = 0;
result = "{\n";
jsonObject.forEach(element => {
if (arrayLen > 0) {
result += ",\n";
}
result += contentTabs + jsontolua(element, contentTabs);
arrayLen ++;
});
result += "\n" + tabs + "}";
arrayLen ++;
}
else if (jsonObject instanceof Object)
{
var arrayLen = 0;
result = "{\n";
for(var key in jsonObject) {
if (arrayLen > 0) {
result += ",\n";
}
result += contentTabs + "[\"" + key + "\"] = " + jsontolua(jsonObject[key], contentTabs);
arrayLen ++;
}
result += "\n" + tabs + "}";
arrayLen ++;
}
else if (typeof jsonObject == "string")
{
result = "\"" + jsonObject.replace(/\"/g, "\\\"") + "\"";
}
else
{
result = "" + jsonObject;
}
}
return result;
}
function myFunction() {
json = document.getElementById("json").value;
try {
var jsonObject = JSON.parse(json);
document.getElementById("lua").value = "return " + jsontolua(jsonObject);
}
catch (ex) {
document.getElementById("lua").value = "ERROR! " + ex;
}
}
</script>
</head>
<body>
<div class="pagediv">
<textarea id="json">
</textarea>
<div class="buttonbar">
<button type="button" onclick="myFunction()">Convert JSON to LUA</button>
</div>
<textarea id="lua">
</textarea>
</div>
</body>
</html>