json数据处理例子

前提:引入json.js,官网下载

<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script src="json.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script>
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
  { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
  { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
  ],
  "authors": [
  { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
  { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
  { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
  ],
  "musicians": [
  { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
  { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
  ] };
alert(people.programmers[0].lastName);//访问成员people.programmers[0].lastName
alert( people.toJSONString()); //访问成员数组的长度people.programmers.length,只有是数组的才有length属性
var jsonStr=people.toJSONString();转化成字符串people.toJSONString()变量jsonStr
var jsonObj=eval("("+jsonStr+")");//将 jsonStr转成json对象 ;注意要加括号


 
</script>
</head>


<body>
<h2>json数据处理例子</h2>
<p>前提:引入json.js文件,官网提供</p>
<p>访问成员people.programmers[0].lastName</p>
<p>访问成员数组的长度people.programmers.length,只有是数组的才有length属性</p>
<p>转化成字符串people.toJSONString()变量jsonStr</p>
<p>转化成json字符串jsonStr转成json对象:var jsonObj=eval("("+jsonStr+")");注意要加括号</p>
<h2>使用$.each处理json:</h2>
<div id="show">each输出结果为:</div>

<script>

// 注意,这个要放在文件后,否则如果div未加载则获取不到div用来显示

$.each(people.programmers,function(idx,item){ 
 $("#show").append(" <b>"+item.firstName+"</b>");
});
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值