-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
70 lines (61 loc) · 1.69 KB
/
index.html
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
<html>
<head>
<title>Sort JSON Object by Key</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body ng-app="myApp">
<div ng-controller="SortJsonCtrl" class="container">
<h3>Sort JSON Object By Key</h3>
<div class="form-group">
<label for="obj">JSON Object</label>
<textarea class="form-control" ng-model="data" id="obj"></textarea>
</div>
<button class="btn btn-success" ng-click="sort(data)">Sort</button>
<br/><br/>
<pre>
{{result | json}}
</pre>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller("SortJsonCtrl", function($scope) {
$scope.sort = function(newVal) {
if(newVal) {
var myObj,
keys = [],
k, i, len;
try {
myObj = angular.fromJson(newVal);
} catch(err) {
try {
newVal = newVal.replace(/'/g, '"');
newVal = newVal.replace(/value/g, '"value"');
myObj = angular.fromJson(newVal);
} catch(err) {
alert("Could not parse your JSON");
}
}
for (k in myObj)
{
if (myObj.hasOwnProperty(k))
{
keys.push(k);
}
}
keys.sort();
len = keys.length;
$scope.result = {};
for (i = 0; i < len; i++)
{
k = keys[i];
$scope.result[k] = myObj[k];
}
}
};
});
</script>
</body>
</html>