Some time before, a small item was made to add a JSON data to automatically bind the function of the front desk form, similar to the data binding of the jQuery-EasyUI (give it a JSON object, you can automatically analyze the data inside. The key of JSON data for the name attribute of the form can also be an ID or the label attribute defined by myself, and assign the corresponding value to the corresponding name form). There are many JSON parsing on Baidu. They are based on the analysis of the JSON structure. There is no use of the wool at all. Finally, I read a blog and understand the principle of analytical key and value. I wrote a simple code (inside inside JSON data comes from the Internet, haha), not much nonsense, the code:
<! Doctype html>
<head>
<Title> Recursive analysis of all key and values </title>
<script src = "jquery-2.0.3.min.js" type = "text/javascript" charset = "utf-8"> </script> </script>
</head>
<body>
<div>
<ul ID = "Menu"> </ul>
</div>
<script>
var menulist = [{{
"Menulist": [[
{"Mid": "m001", "mname": "Homepage", "URL": "#", "Menulist": ""},
{"Mid": "m002", "mname": "Vehicle buy and sell", "url": "#", "menulist":
{"Mid": "m003", "mname": "new car", "url": "#", "menulist":
{"Mid": "m006", "mname": "Audi", "url": "#", "menulist": ""},
{"Mid": "m007", "mname": "Buick", "url": "#", "menulist": "}
]
},
{"Mid": "m004", "mname": "used car", "url": "#", "menulist": ""},
{"Mid": "m005", "mname": "modified car", "url": "#", "menulist": "}
]
},
{"Mid": "m006", "mname": "pet", "url": "#", "menulist": "}
]
}];
$ (function () {
Parsejson (Menulist);
});
Function Parsejson (JSONOBJ) {
if (typeof (jsonobj) == "underfined") {{
alert ('json object cannot be empty!');
}
// Traversing the first layer of data
for (var topKey in jsonobj) {
// If the object type is Object type and the array length is greater than 0, recursive analysis
if (JSONOBJ [TopKey] .Length> 0 && Typeof (jsonobj [Topkey]) == "Object") {
Parsejson (jsonObj [Topkey]);
} Else {
// If the object type is Object type, take all elements in turn
if (typeof (jsonobj [Topkey]) == "Object") {{
for (var childkey in jsonobj [topkey]) {{
// If the object type is Object type, recursive analysis
ifof (jsonobj [topKey] [childkey] == "object") {
Parsejson (jsonObj [Topkey] [Childkey]);
} Else {
// If the object type is Object type, take the element name and value directly
$ ("#MENU"). APPEND ("<li>" + Childkey + ":" + JSONOBJ [Topkey] [Childkey] + "</li>");
}
}
} Else {
// If the object type is Object type, take the element name and value directly
$ ("#MENU"). APPEND ("<li>" + Childkey + ":" + JSONOBJ [Topkey] [Childkey] + "</li>");
}
}
}
}
</script>
</body>
</html>
code is very simple, only use for loop, recursion, and some JSON knowledge points, jquery can be easy to understand if you do n’t understand (if you do n’t understand, please refer to this blog.Address), please point out if you need to improve or have incomplete considerations.