通过这个JSON迭代?

时间:2016-06-04 17:02:44

标签: javascript json node.js

我是JS的新手,所以这与我习惯使用的典型数据结构有点不同。下面是我试图迭代的简单代码片段。我已经尝试了一些示例代码,至少要解析JSON中的信息,但它并没有按照我想要的方式工作。

    [{
        "data": ["\t\tThe Catcher In The Rye by J. D. Salinger[Team Nanban][TPB]\n", "magnet:?xt=urn:btih:d87b74de181abda5dc54be34713473ae025e5ea1&dn=The+Catcher+In+The+Rye+by+J.+D.+Salinger%5BTeam+Nanban%5D%5BTPB%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
        "epub_convert": 0
    }, {
        "data": ["\t\tThe Catcher In The Rye (Corrected and Updated)\n", "magnet:?xt=urn:btih:a5db6afb1eb238fa990b02377c38088e333609ad&dn=The+Catcher+In+The+Rye+%28Corrected+and+Updated%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
        "epub_convert": 0
    }, {
        "data": ["\t\tTHE CATCHER IN THE RYE - J. D. Salinger.pdf\n", "magnet:?xt=urn:btih:6c146dd22323e604b74a326d23122a812a107466&dn=THE+CATCHER+IN+THE+RYE+-+J.+D.+Salinger.pdf&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
        "epub_convert": 0
    }, {
        "data": ["\t\tCatcher In The Rye (Final Proof)\n", "magnet:?xt=urn:btih:faee3c58e847c0f2f8df00a3b5be870daa2e0dee&dn=Catcher+In+The+Rye+%28Final+Proof%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
        "epub_convert": 0
    }, {
        "data": ["\t\tJ.D. Salinger:The Catcher in the Rye pdf, e-pub, mobi\n", "magnet:?xt=urn:btih:3ccfcbfbdf1d835776c4a1b02c60acf5f2dc036d&dn=J.D.+Salinger%3AThe+Catcher+in+the+Rye++pdf%2C+e-pub%2C+mobi&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
        "epub_convert": 1
    }]

我正在尝试迭代这个JSON,但似乎无法使格式正确。我怎样才能以干净的方式循环获取标题,链接和epub_convert值?

#Starter Attempts
           for (var i = 0; i < data.length; i++){
             console.log(JSON.parse(data[i]));
           }

________
           for (var i = 0; i < data.length; i++){
             console.log(data[i]);
           }
_________

    console.log(data[i]['data']['epub_convert']);

1 个答案:

答案 0 :(得分:1)

只需使用 forEach() 来迭代数组元素

var data = [{
  "data": ["\t\tThe Catcher In The Rye by J. D. Salinger[Team Nanban][TPB]\n", "magnet:?xt=urn:btih:d87b74de181abda5dc54be34713473ae025e5ea1&dn=The+Catcher+In+The+Rye+by+J.+D.+Salinger%5BTeam+Nanban%5D%5BTPB%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tThe Catcher In The Rye (Corrected and Updated)\n", "magnet:?xt=urn:btih:a5db6afb1eb238fa990b02377c38088e333609ad&dn=The+Catcher+In+The+Rye+%28Corrected+and+Updated%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tTHE CATCHER IN THE RYE - J. D. Salinger.pdf\n", "magnet:?xt=urn:btih:6c146dd22323e604b74a326d23122a812a107466&dn=THE+CATCHER+IN+THE+RYE+-+J.+D.+Salinger.pdf&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tCatcher In The Rye (Final Proof)\n", "magnet:?xt=urn:btih:faee3c58e847c0f2f8df00a3b5be870daa2e0dee&dn=Catcher+In+The+Rye+%28Final+Proof%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\t\tJ.D. Salinger:The Catcher in the Rye pdf, e-pub, mobi\n", "magnet:?xt=urn:btih:3ccfcbfbdf1d835776c4a1b02c60acf5f2dc036d&dn=J.D.+Salinger%3AThe+Catcher+in+the+Rye++pdf%2C+e-pub%2C+mobi&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 1
}];

data.forEach(function(v) {
  console.log(
    v.data[0], // title
    v.data[1], // url
    v.epub_convert // epub_convert value
  );
});

如果它是一个字符串,那么你需要先解析它,然后使用JSON.parse()方法。

var json = `[{
  "data": ["\\t\\tThe Catcher In The Rye by J. D. Salinger[Team Nanban][TPB]\\n", "magnet:?xt=urn:btih:d87b74de181abda5dc54be34713473ae025e5ea1&dn=The+Catcher+In+The+Rye+by+J.+D.+Salinger%5BTeam+Nanban%5D%5BTPB%5D&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tThe Catcher In The Rye (Corrected and Updated)\\n", "magnet:?xt=urn:btih:a5db6afb1eb238fa990b02377c38088e333609ad&dn=The+Catcher+In+The+Rye+%28Corrected+and+Updated%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tTHE CATCHER IN THE RYE - J. D. Salinger.pdf\\n", "magnet:?xt=urn:btih:6c146dd22323e604b74a326d23122a812a107466&dn=THE+CATCHER+IN+THE+RYE+-+J.+D.+Salinger.pdf&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tCatcher In The Rye (Final Proof)\\n", "magnet:?xt=urn:btih:faee3c58e847c0f2f8df00a3b5be870daa2e0dee&dn=Catcher+In+The+Rye+%28Final+Proof%29&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 0
}, {
  "data": ["\\t\\tJ.D. Salinger:The Catcher in the Rye pdf, e-pub, mobi\\n", "magnet:?xt=urn:btih:3ccfcbfbdf1d835776c4a1b02c60acf5f2dc036d&dn=J.D.+Salinger%3AThe+Catcher+in+the+Rye++pdf%2C+e-pub%2C+mobi&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Fopen.demonii.com%3A1337&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Fexodus.desync.com%3A6969"],
  "epub_convert": 1
}]`;

var data = JSON.parse(json);

data.forEach(function(v) {
  console.log(
    v.data[0], // title
    v.data[1], // url
    v.epub_convert // epub_convert value
  );
});

UPDATE:您需要像JSON.parse(return_data.responseText)一样解析它,因为json字符串包含responseText属性。