<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>json test 1</title> <mce:script src="../js/jquery-1.4.1.min.js" mce_src="js/jquery-1.4.1.min.js" type="text/javascript"></mce:script> <mce:script src="../js/jquery-1.4.1.min-vsdoc.js" mce_src="js/jquery-1.4.1.min-vsdoc.js" type="text/javascript"></mce:script> <mce:script type="text/javascript"><!-- //1.json对象 var o = { "xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime": "2009-08-20" }; //2.json数组 var jsonranklist = [{ "xlid": "cxh", "xldigitid": 123456, "topscore": 2000, "topplaytime": "2009-08-20" }, { "xlid": "zd", "xldigitid": 123456, "topscore": 1500, "topplaytime": "2009-11-20"}]; //3.json字符串 var str1 = '{ "name": "cxh", "sex": "man" }'; //4.json对象 var str2 = { "name": "cxh", "sex": "man" }; //5.json字符串转json对象 var obj = eval('(' + str1 + ')'); //alert("姓名:" + obj.name + "/t性别:" + obj.sex); var studentJsonStr = '[{"name":"王小明","sex":"男","ok":true,"age":18},{"name":"刘小壮","sex":"女","ok":false,"age":28},{"name":"小燕子","sex":"女","ok":true,"age":29}]'; var arr = eval(studentJsonStr); var tempHtml = ""; for (var i = 0; i < arr.length; i++) { tempHtml += "<tr><td>" + arr[i].name + "</td><td>" + arr[i].sex + "</td><td>" + arr[i].ok + "</td><td>" + arr[i].age + "</td></tr>"; } $(function() { $("#tb").html(tempHtml); //由字符串得到json var obj2 = eval('(' + studentJsonStr + ')'); //alert("json子对象个数:" + obj2.root.length); $.each(obj2, function(idx, item) { alert(item.name + " " + item.sex + " " + item.ok + " " + item.age); }); }); // --></mce:script> </head> <body> <table id="table1" cellpadding="2" cellspacing="2" border="1" > <thead> <tr><th>姓名</th><th>性别</th><th>好人</th><th>年龄</th></tr> </thead> <tbody id="tb"> </tbody> </table> </body> </html>