简体   繁体   English

使用AJAX读取JSON文件并将数据存储到数组

[英]Reading JSON file with AJAX and storing data to an array

All I am trying to do is reading an JSON file with data format as shown below and extract only id from nodes and store it in an array. 我要做的就是读取具有如下所示数据格式的JSON文件,并仅从节点提取ID并将其存储在数组中。

{"nodes":[
{"id":"1057457211927117824", "age":"20", "name":"a", "loaded":true},
{"id":"1057459284189970433", "age":"20", "name":"b", "loaded":true}
]
"links":[
{"id":"l01", "from":"1057457210467540992", "to":"1057455883972722689", "type":"friend"},
{"id":"l02", "from":"1057457271331057664", "to":"1057451606344646656", "type":"friend"}
]}

Below is the code I tried but its not working. 以下是我尝试过的代码,但无法正常工作。

var node_id = []; 
$.getJSON("data/newData.json", function (data) {
    $.each(data, function (index, value) {
        node_id.push(value[0]['id']); 
    });
});

If I understand you, you can look at the code below: 如果您了解我的意思,可以查看以下代码:

 var jsonData = { "nodes":[ {"id":"1057457211927117824", "age":"20", "name":"a", "loaded":true}, {"id":"1057459284189970433", "age":"20", "name":"b", "loaded":true}], "links":[ {"id":"l01", "from":"1057457210467540992", "to":"1057455883972722689", "type":"friend"}, {"id":"l02", "from":"1057457271331057664", "to":"1057451606344646656", "type":"friend"} ]}; var node_id = []; $.each(jsonData.nodes, function (index, value) { node_id.push(value['id']); }); console.log(node_id); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM