简体   繁体   English

如何读取我的 JSON 文件?

[英]How do I read my JSON file?

I've been working on it for hours, reading stackoverflow articles but I cannot figure it out.我已经研究了几个小时,阅读stackoverflow文章,但我无法弄清楚。 I'm trying to read information from a .json file into an array.我正在尝试将 .json 文件中的信息读取到数组中。 The .json file has one key mapped to a huge array of JSON values...here is the code: .json 文件有一个键映射到一个巨大的 JSON 值数组......这是代码:

JSON file: JSON 文件:

{
"SDpairs":
[{"SD Pair":"Afghanistan -
Azerbaijan","Source":"Afghanistan","Dest":"Algeria","Flow":5,"Capacity":16},
{"SD Pair":"Afghanistan - 
Azerbaijan","Source":"Afghanistan","Dest":"Australia","Flow":3,"Capacity":3},
{"SD Pair":"Afghanistan - Azerbaijan","Source":"Afghanistan","Dest":"Austria","Flow":12,"Capacity":12},
{"SD Pair":"Afghanistan - Azerbaijan","Source":"Afghanistan","Dest":"Belgium","Flow":3,"Capacity":10},

etc...等等...

JavaScript method: JavaScript 方法:

$.getJSON("SDpairs.json", function(data) {

    var str = JSON.stringify(data);
    var json = JSON.parse(str);
    console.log(json);
    console.log("Type of 'json': " + typeof json);

    for (var edge in json["SDpairs"]) {
        console.log(json["SDPairs"][edge]);
        console.log("Type of 'edge': " + typeof edge);
        if (currPair == null) {
            // If it is the first JSON to be read
            currPair = [];
            currPair.push(edge);
        } else if (edge['SD Pair'] == currPair[0]['SD Pair']) {
            // If the JSON is part of the same SD pair as currPair, push into currPair
            currPair.push(edge);
        } else {
            // If it is a new SDpair as compared to currPair
            sdPairs.push(currPair);
            currPair = [];
            currPair.push(edge);
        }
    }

}

EDIT:编辑:

Errors:错误:

 Uncaught TypeError: Cannot read property '0' of undefinedinitData @     
 database.js:67(anonymous function) @ index.html:31
 database.js:10 Object
 database.js:11 Type of 'json': object
 database.js:14 Uncaught TypeError: Cannot read property '0' of     
 undefined(anonymous function) @ database.js:14f.Callbacks.o @    
 jquery.min.js:2f.Callbacks.p.fireWith @ jquery.min.js:2w @ 
 jquery.min.js:4f.support.ajax.f.ajaxTransport.c.send.d @ jquery.min.js:4

Update #1:更新 #1:

var sdPairs = [];

var initData = function() {
var currPair = [];

// Create and push arrays of JSONs into sdPairs. 
// Each array contains edges from the same SD Pair
$.getJSON("SDpairs.json", function(data) {

    var num = 0;
    for (var element in data["SDpairs"]) {
        var edge = data["SDpairs"][element];

        if (currPair.length == 0) {
            // If it is the first JSON to be read
            currPair.push(edge);
        } else if (edge['SD Pair'] == currPair[0]['SD Pair']) {
            // If the JSON is part of the same SD pair as currPair, push into currPair
            currPair.push(edge);
        } else {
            // If it is a new SDpair as compared to currPair
            sdPairs.push(currPair);
            currPair = [];
            currPair.push(edge);
        }

        if (num == 0) {
            console.log("Edge is " + edge);
            console.log("edge['SD Pair'] is " + edge['SD Pair']);
        }
        num++;
    }
    sdPairs.push(currPair);
});
console.log(sdPairs.length);
console.log(currPair.length);
}

It looks like you're not getting the right output, for two reasons.看起来你没有得到正确的输出,有两个原因。 You're only comparing keys (not their values) in your for..in loop.您只是在for..in循环中比较键(而不是它们的值)。 You're also losing out on the last processed set of objects when exiting the loop.退出循环时,您还会丢失最后处理的一组对象。 You should add it at the end.您应该在最后添加它。

var sdPairs=[], currPair; // Remember to declare the variables you're using somewhere
for(var name in json["SDpairs"]) {
    var edge = json["SDpairs"][name];
    // The rest of your code
}
sdPairs.push(currPair);
console.log(sdPairs); // Do something with sdPairs now

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

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