使用jQuery浏览JSON API树

时间:2015-08-20 22:58:37

标签: javascript jquery json

我正在尝试使用JSON api并检索一些数据。我仍然是新手,我似乎无法弄清楚使用哪个值。我的JSON API看起来像这样:

public boolean equals (Object rhs) {          
    if (this == rhs) { 
        return true; 
    }          
    if (!(rhs instanceof Building)) { 
        return false; 
    }          
    Building b = (Building) rhs;     

    // This is what you're supposed to add. It will return true only if both
    // object's attributes (name and number of floors) are the same
    return this.name.equals(b.name) && this.noOfFloors == b.noOfFloors;
} 

我的jQuery是:

[
   {"lang":"english","visual":"<span>Text</span>","weight":0.92},
   {"lang":"swedish","visual":"<span>Text</span>","weight":0.22},
   //etc
]

但没有记录任何内容。如何浏览JSON树?感谢

1 个答案:

答案 0 :(得分:1)

data.lang未定义。 lang是数据保存的对象数组中每个对象的属性。只需迭代数据数组,每个对象都将包含可视属性(以及lang);

$.getJSON(url ,function(data) {
    $.each(data, function() {
        var lang = this["lang"];
        var dataName = this["visual"];
        console.log(dataName);
    });
});