getJSON()方法读取。 用法如下

$.getJSON(url,[data],[callback])

  url:加载的页面地址 
  data: 可选项,发送到服务器的数据,格式是key/value 
  callback:可选项,加载成功后执行的回调函数 

  我们来看一下实例,注意本例是将json和调用页面放在同一个服务器,如果没在一起可能会出现跨域的错误,CORS策略阻止

  案例一、

  首先建一个JSON格式的文件userinfo.json 保存用户信息。如下: 

[ 
{ 
"name":"Leo", 
"sex":"男", 
"email":"leo@123.com"
}, 
{ 
"name":"Paul", 
"sex":"男", 
"email":"paul@123.com"
}, 
{ 
"name":"Linda", 
"sex":"女", 
"email":"linda@123.com"
} 
]

  然后建一个页面用于获取JSON文件里的用户信息数据,并显示

<!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=utf-8" /> 
<title>getJSON获取数据</title> 
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/1.8.2/jquery.min.js"></script> 
<style type="text/css"> 
#divframe{ border:1px solid #999; width:500px; margin:0 auto;} 
.loadTitle{ background:#CCC; height:30px;} 
</style> 
< script type = "text/javascript" > 
$(function (){
  $("#btn").click(function ()  {
    $.getJSON("js/userinfo.json", function (data){
      var $jsontip = $("#jsonTip");
      var strHtml = "123";
      //存储数据的变量 
      $jsontip.empty();
      //清空内容 
      $.each(data, function (infoIndex, info){
        strHtml += "姓名:" + info["name"] + "<br>";
        strHtml += "性别:" + info["sex"] + "<br>";
        strHtml += "邮箱:" + info["email"] + "<br>";
        strHtml += "<hr>" 
      }) 
      $jsontip.html(strHtml);
      //显示处理后的数据 
    }) 
  }) 
})
</script> 
</head> 
<body> 
<div id="divframe"> 
<div class="loadTitle"> 
<input type="button" value="获取数据" id="btn"/> 
</div> 
<div id="jsonTip"> 
</div> 
</div> 
</body> 
</html>

  案例二、

通过ajax获取json数据,我们再来看一个例子,创建一个

[
{ "img": "https://images.cnblogs.com/cnblogs_com/ytkah/532212/t_weixinkaifa.jpg", "url":"https://images.cnblogs.com/cnblogs_com/ytkah/532212/t_weixinkaifa.jpg" },
{ "img": "https://images.cnblogs.com/cnblogs_com/ytkah/532212/t_weixinkaifa1.png", "url":"https://images.cnblogs.com/cnblogs_com/ytkah/532212/t_weixinkaifa1.png" },
{ "img": "http://images.cnblogs.com/cnblogs_com/ytkah/532212/t_微信.png", "url":"http://images.cnblogs.com/cnblogs_com/ytkah/532212/t_微信.png" }
]

  然后通过ajax调用json数据

<!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=utf-8" />
<title>通过ajax获取json数据的实现代码</title>
<script type="text/javascript" src="https://cdn.bootcdn.net/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
</head>
<body>
<div id="ok"></div>
<script>
$(function () {
    $.ajax({
      type: "POST",
      dataType: "json",
      url: "da.json",
      success: function (result) {
        var str = "";
                $.each(result,function(index,obj){              
                str += "<a href='" + obj["url"] + "' target='_blank'><img src='" + obj["img"] + "' /></a>";                   
                });
        $("#ok").append(str);
      }
    });
});
</script>
</body>
</html>

  通过$.getJSON获取json的代码

<!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=utf-8" />
<title>通过$.getJSON获取json的代码</title>
<script type="text/javascript" src="//www.jb51.net/jslib/jquery/jquery.min.js"></script>
</head>
<body>
<div id="ok"></div>
<script>
$(function(){ 
$.getJSON("da.json",function(data){ 
var $jsontip = $("#ok"); 
var strHtml = "";//存储数据的变量 
$jsontip.empty();//清空内容 
$.each(data,function(infoIndex,info){
    strHtml += "<a href='" + info["url"] + "' target='_blank'><img src='" + info["img"] + "' /></a>";
}) 
$jsontip.html(strHtml);//显示处理后的数据 
}) 
}) 
</script>
</body>
</html>

  如果出现乱码记得修改一下文件的编码重新上传一次试试