json

获取json步骤

1 首先,我们将保存一个即将访问的 URL 作为变量。在您的 JavaScript 代码的底部添加代码

var requestURL =*********;

2 为了创建一个HTTP请求,我们需要创建一个HTTP请求对象,通过 new 构造函数的形式。在您最下面的代码中写入:

var request = new XMLHttpRequest();

3 现在我们需要使用open 函数打开一个新的请求,添加如下代码:

request.open('GET', requestURL);

4 接下来,添加,两行代码,我们设定 responseType 为 JSON,所以服务器将知道我们想要返回一个 JSON 对象,然后发送请求 :

request.responseType = 'json';
request.send();

5最后一点内容涉及相应来自服务器的返回数据,然后处理它,添加如下代码在您先前的代码下方:

request.onload = function() {
  var superHeroes = request.response;
  populateHeader(superHeroes);
  showHeroes(superHeroes);
}

 

Json文件

{
  "Name" : "ROSE",
  "homeTown" : "Chicago City",
  "formed" : 2008,
  "location" : "Pg",
  "active" : true,
  "members" : [
    {
      "name" : "G Bson",
      "age" : 34,
      "location" : "PF",
      "speciallity" : [
        "Points",
        "Rebounce",
      ]
    },
    {
      "name" : "Deng",
      "age" : 34,
      "location" : "PF",
      "speciallity" : [
        "Points",
        "Rebounce",

      ]
    },
    {
      "name" : "dige",
      "age" : 29,
     "location" : "sf",
      "speciallity" : [
        "Points",
        "Rebounce",

      ]
    }
  ]
}

 
posted @ 2018-11-25 16:46  Gabriel-  阅读(83)  评论(0编辑  收藏  举报