1. var jsonStr = ‘{"a": 1, "b": "string", "c": [1, "11"]}‘; console.log(typeof(jsonStr)); // string var json = $.parseJSON(jsonStr); console.log(typeof(json)); // object console.log(json.c); // [1, "11"] 2. <script> $(function(){ var jsonStr = ‘{"id": 1, "name": "zhansan", "addr": [22, "33"]}‘; var json = $.parseJSON(jsonStr); var nd1 = "<td>"+json.id+"</td>"; var nd2 = "<td>"+json.name+"</td>"; var nd3 = "<td>"+json.addr[0]+"</td>"; var nd4 = "<td>"+json.addr[1]+"</td>"; var s = nd1+nd2+nd3+nd4; $("tr:eq(0)").append(s); }); </script> 3. <script> $(function(){ var jsonStr = ‘[{ "id" : 1 , "name" : "xiaoming"},‘ + ‘{"id" : 2 , "name" : "xiaohong"}, ‘+ ‘{"id" : 3 , "name" : "xiaowang"}]‘; var json = $.parseJSON(jsonStr); //alert(json.length); for(var i=0;i<json.length;i++){ $nd1 = "<td>"+json[i].id+"</td>" ; $nd2 = "<td>"+json[i].name+"</td>" ; $node = "<tr>"+$nd1 + $nd2+"</tr>"; $("table:eq(0)").append($node); } }); </script> <table width="600" border="1"> </table>
原文地址:https://www.cnblogs.com/yoyo198212/p/8848631.html
时间: 2024-11-09 10:13:27