jQuery遍历JSON对象 [英] jQuery loop through JSON objects

查看:115
本文介绍了jQuery遍历JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历此JSON文件(下面的结构),并获取所有国家(例如,奥地利)的酒店.使用getJson(),因此也无法更改JSON文件中的任何内容.

I want to loop through this JSON file (structure below) and get all Hotels where country is Austria for example. Using getJson() so am unable to change anything in the JSON file too.

任何帮助将不胜感激.

[
  {
    "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并检查酒店是否在奥地利并记录名称和城市进入控制台:

I don't know what exactly you want to do, but here's a working example that loops the json and checks if the hotel is in Austria and logs the name and city into the console:

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

这篇关于jQuery遍历JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆