1 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 6 <title>JOSN演示</title> 7 <script src="component/jquery-1.11.1.js"></script> 8 <script> 9 $(document).ready(function() { 10 var jsonStr = "{‘name‘: ‘cxh‘, ‘sex‘: ‘man‘, ‘age‘: 1}"; 11 var obj = eval(‘(‘ + jsonStr + ‘)‘); 12 var traverseJson="{ ‘name‘: ‘cxh‘, ‘sex‘: ‘man‘, ‘age‘: 1},解析结果:</br>"; 13 for(var a in obj) { 14 traverseJson += "key: " + a +",value: " + obj[a]+" </br> "; 15 } 16 $("#traverseJson").html(traverseJson); 17 var arrayStr = [{"name":"运维管理","tId":"roleMenu0_1","children":[{"name":"权限管理","tId":"roleMenu0_2","children":[{"name":"角色管理","tId":"roleMenu0_3"},{"name":"用户管理","tId":"roleMenu0_4"}]},{"name":"菜单分配","tId":"roleMenu0_5"}]}]; 18 var str = ""; 19 f(arrayStr, str); 20 }); 21 22 function f(array, str) { 23 str += "--"; 24 for (var i = 0; i < array.length; i++) { 25 var object = array[i]; 26 if (object.children) { 27 $("#traverseArray").append(str + object.name +" </br> "); 28 f(object.children, str); 29 } else { 30 $("#traverseArray").append(str + object.name +" </br> "); 31 } 32 } 33 } 34 </script> 35 </head> 36 <body> 37 <font color="red" id="traverseJson"></font> 38 <hr/> 39 <font color="red" id="traverseArray"></font> 40 </body> 41 </html>
输出结果为:
时间: 2024-10-23 06:54:20