jQuery循环遍历JSON对象

时间:2016-11-21 01:58:33

标签: javascript jquery json

我想循环浏览此JSON文件(下面的结构)并获取所有国家/地区为奥地利的酒店。使用getJson()因此无法更改JSON文件中的任何内容。

非常感谢任何帮助。

[
  {
    "Site ID": 19955,
    "Hotels": "Ramada Salzburg City Centre",
    "Stadt": "Salzburg",
    "Country": "Austria",
    "Region": "Central & Eastern Europe",
    "Link DE": "",
    "Link EN": "",
    "Link TR": "",
    "Lat": 47.8137521,
    "Long": 13.044259,
    "Image": "/Salzburg.jpg"
  }, {
    "Site ID": 1211,
    "Hotels": "test",
    "Stadt": "Salzburg",
    "Country": "NZ",
    "Region": "Central & Eastern Europe",
    "Link DE": "",
    "Link EN": "",
    "Link TR": "",
    "Lat": 47.8137521,
    "Long": 13.044259,
    "Image": "/Salzburg.jpg"
  }
]

3 个答案:

答案 0 :(得分:2)

我不知道你到底想做什么,但这里有一个循环json并检查酒店是否在奥地利并将名称和城市记录到控制台的工作示例:

var json = [{
  "Site ID": 19955,
  "Hotels": "Ramada Salzburg City Centre",
  "Stadt": "Salzburg",
  "Country": "Austria",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}, {
  "Site ID": 1211,
  "Hotels": "test",
  "Stadt": "Salzburg",
  "Country": "NZ",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}];

$(json).each(function () {
  if (this.Country === "Austria") {
    console.log("Found hotel " + this.Hotels + " in " + this.Stadt);
  }
});

答案 1 :(得分:0)

尝试数组map()filter()方法:



var json = [{
  "Site ID": 19955,
  "Hotels": "Ramada Salzburg City Centre",
  "Stadt": "Salzburg",
  "Country": "Austria",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}, {
  "Site ID": 1211,
  "Hotels": "test",
  "Stadt": "Salzburg",
  "Country": "NZ",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}];

var austriaHotels = json.filter(function(item) {
  return item.Country == 'Austria';
});

var hotelsName = austriaHotels.map(function(item) {
  return item.Hotels;
});

console.log(hotelsName);




答案 2 :(得分:-1)

您需要存储您的位置,循环它们并将匹配的位置组合成一个新阵列。

var locations = [{
  "Site ID": 19955,
  "Hotels": "Ramada Salzburg City Centre",
  "Stadt": "Salzburg",
  "Country": "Austria",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}, {
  "Site ID": 1211,
  "Hotels": "test",
  "Stadt": "Salzburg",
  "Country": "NZ",
  "Region": "Central & Eastern Europe",
  "Link DE": "",
  "Link EN": "",
  "Link TR": "",
  "Lat": 47.8137521,
  "Long": 13.044259,
  "Image": "/Salzburg.jpg"
}];

// Store the matched hotels here
var matches = [];

// Loop over hotels 
for( i=0; i<locations.length; i++ ) {

    // Check if the location is in Austria, if so push it to our matches array
    if( locations[i].Country == 'Austria' ) {
        matches.push(locations[i]);
    }
}

// Check for matched hotels
console.log( matches );