我如何阅读我的JSON文件?

时间:2015-10-07 21:35:53

标签: javascript json

我已经工作了几个小时,阅读stackoverflow文章,但我无法弄明白。我试图将.json文件中的信息读入数组。 .json文件有一个键映射到一大堆JSON值...这里是代码:

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},

等...

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);
        }
    }

}

编辑:

错误:

 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

更新#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);
}

1 个答案:

答案 0 :(得分:1)

看起来你没有得到正确的输出,原因有两个。您只是在for..in循环中比较键(而不是它们的值)。退出循环时,您还会丢失最后处理的对象集。你应该在最后添加它。

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
相关问题