Javascript - 如何获取清单json并将其用作对象?

时间:2017-03-10 00:38:56

标签: javascript json

为什么console.log('Your query count: ' , data);显示为空? (即使结果是成功的?调试控制台网络选项卡显示数据已成功爬行)

enter image description here

的manifest.json:

{
  "title": "API test",
  "server": [
    { "agent": "abc",           
      "url": "def" },        
  ],
}

的index.php:

<script>
var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        callback(null, xhr.response);
      } else {
        callback(status);
      }
    };
    xhr.send();
};

getJSON('http://localhost/manifest.json',
function(err, data) {
  if (err != null) {
    alert('Something went wrong: ' + err);
  } 
  else {
    var read = JSON.stringify(data);
    console.log('Your query count: ' , data);
  }
});
</script>

2 个答案:

答案 0 :(得分:2)

您的代码适合我,但请尝试

var read = JSON.stringify(data);
console.log('Your query count: ' , read );

而不是

var read = JSON.stringify(data);
console.log('Your query count: ' , data);

编辑:哦,看起来你的manifest.json不是正确的JSON。尝试删除逗号

{
 "title": "API test",
 "server": [{
    "agent": "abc",
    "url": "def"
 }]
}

答案 1 :(得分:1)

你的代码没问题。 但是,我注意到你的Json最后有一个,

{ 
  "title": "API test",
  "server": [
    { "agent": "abc",           
      "url": "def" },        
  ],<-- this one
}

只需删除它,您的代码即可运行。 同样在getJSON的匿名函数的else运算符中,用data替换变量read,您将获得JSON作为字符串,或者您可以从data变量获取数据。在这里,您有3种方法可以获取数据。

var getJSON = function(url, callback) {
  var xhr = new XMLHttpRequest();
  xhr.open('GET', url, true);
  xhr.responseType = 'json';
  xhr.onload = function() {
    var status = xhr.status;
    if (status == 200) {
      callback(null, xhr.response);
    } else {
      callback(status);
    }
  };
  xhr.send();
};

getJSON('https://gist.githubusercontent.com/teocci/3d128c27e37cc5d9b90ade9d68e84ca7/raw/e49f9d765567572aa4119142dc46ea5cc55d9b15/manifest.json%2520',
  function(err, data) {
    if (err != null) {
      alert('Something went wrong: ' + err);
    } else {
      var read = JSON.stringify(data);
      console.log('Your query count: ', read);
      getInfoA(data);
      getInfoB(data);
      getInfoC(data);
    }
  });

function getInfoA(jsonData) {
  var title = jsonData.title;
  var server = jsonData.server;
  var agent = server[0].agent;
  var url = server[0].url;

  console.log('getInfoA---');

  console.log('title: ', title);
  console.log('agent: ', agent);
  console.log('url: ', url);
}

function getInfoB(jsonData) {
  var title = jsonData.title;
  var server = jsonData.server;

  console.log('getInfoB---');
  console.log('title: ', title);

  Object.keys(server).map(function(key, index) {
    var item = server[key];
    var agent = item.agent;
    var url = item.agent;
    console.log('agent: ', agent);
    console.log('url: ', url);
  });
}

function getInfoC(jsonData) {
  var title = jsonData.title;
  var server = jsonData.server;
  console.log('getInfoC---');
  console.log('title: ', title);
  Object.keys(server).forEach(function(key) {
    var item = server[key];
    var agent = item.agent;
    var url = item.agent;
    console.log('agent: ', agent);
    console.log('url: ', url);
  });
}