jquery处理json笔记

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>

  

posted @ 2018-04-15 18:11  yoyo198212  阅读(67)  评论(0编辑  收藏  举报