HTML-readJSON

HTML-readJSON

JSON对象及JSON数组的引用方式差异
两种获取值的方式item[“name”]及item.name
JSON文件

[
{
 "tasksess":"task1",
 "search":[{
  "ref":{
   "srcid":"src1",
   "range":"14 139 92 334",
   "degree":"0.9",
   "offset":"00:00:05"
   },
  "devid":"dev1",
  "dstpic":"pic1"
 }],
 "result":"done"
},{
 "tasksess":"task2",
 "search":[{
  "ref":{
   "srcid":"src2",
   "range":"14 139 92 334",
   "degree":"0.9",
   "offset":"00:00:06"
   },
  "devid":"dev2",
  "dstpic":"pic2"
 }],
 "result":"none"
}
]

jquery读取代码

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>jqueryJSON</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
 var Ajax=function(){
  $.getJSON("objarrJSON.json",function(data){
   $.each(data,function(i,item){
    $("#disp").append("<h3>"+item["tasksess"]+"</h3>");
    $("#disp").append("<p>devid:"+item["search"][0]["devid"]+"</p>");
    $("#disp").append("<p>dstpic:"+item.search[0].dstpic+"</p>");
    $("#disp").append("<p>src:"+item.search[0].ref["srcid"]+"</p>");
    $("#disp").append("<p>offset:"+item.search[0].ref.offset+"</p>");
    $("#disp").append("<p>result:"+item.result+"</p>");
   });
  });
 }
</script>
</head>
<body>
 <input type="button" value="ajaxsubmit" onclick="Ajax();" />
 <div id="disp"></div>
</body>
</html>

效果
读取JSON文件
ajax读取代码

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8" />
<title>ajaxJSON</title>
<script type="text/javascript" src="jquery.js"></script>
 <script>
$(function () {
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "objarrJSON.json",
      success: function (result) {
        $.each(result,function(i,item){
    $("#disp").append("<h3>"+item["tasksess"]+"</h3>");
    $("#disp").append("<p>devid:"+item["search"][0]["devid"]+"</p>");
    $("#disp").append("<p>dstpic:"+item.search[0].dstpic+"</p>");
    $("#disp").append("<p>src:"+item.search[0].ref["srcid"]+"</p>");
    $("#disp").append("<p>offset:"+item.search[0].ref.offset+"</p>");
    $("#disp").append("<p>result:"+item.result+"</p>");
   });
      }
    });
});
</script>
</head>
<body>
 <div id="disp"></div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值