2012-06-04 46 views
0

如何使用javascript和基于提供的输入获取我的JSON数据?如何使用JavaScript和基于输入提供的JSON数据?

我的JSON文件看起来如下...

{ 
    "Name1": { 
     "id": "32313", 
     "latitude": "57.1", 
     "longitude": "9.85", 
     "timezone": "2", 
     "city": "Aalborg", 
     "country": "Denmark", 
     "country_code": "DK" 
    }, 
    "Name2": { 
     "id": "32314", 
     "latitude": "56.15", 
     "longitude": "10.2167", 
     "timezone": "2", 
     "city": "Aarhus", 
     "country": "Denmark", 
     "country_code": "DK" 
    }, 
    "Name3": { 
     "id": "122109", 
     "airport_name": "Aasiaat", 
     "latitude": "68.7", 
     "longitude": "-52.75", 
     "timezone": "-3", 
     "city": "Aasiaat", 
     "country": "Greenland", 
     "country_code": "GL" 
    }, 
    "Name4": { 
     "id": "116713", 
     "latitude": "30.371111", 
     "longitude": "48.228333", 
     "timezone": "4", 
     "city": "Abadan", 
     "country": "Iran", 
     "country_code": "IR" 
    }, 
    "Name5": { 
     "id": "116711", 
     "latitude": "53.74", 
     "longitude": "91.385", 
     "timezone": "7", 
     "city": "Abakan", 
     "country": "Russia", 
     "country_code": "RU" 
    }, 
    "Name6": { 
     "id": "120587", 
     "latitude": "49.05", 
     "longitude": "-122.2833", 
     "timezone": "-7", 
     "city": "Abbotsford", 
     "country": "Canada", 
     "country_code": "CA" 
    }, 
    "Name7": { 
     "id": "116759", 
     "latitude": "13.847", 
     "longitude": "20.844333", 
     "timezone": "1", 
     "city": "Abeche", 
     "country": "Chad", 
     "country_code": "TD" 
    }, 
    "Name8": { 
     "id": "32325", 
     "latitude": "57.2", 
     "longitude": "-2.2", 
     "timezone": "1", 
     "city": "Aberdeen", 
     "country": "United Kingdom", 
     "country_code": "GB" 
    } 
} 

从上面的JSON文件...我需要得到如下:

  1. 获取国名单
  2. 获取根据所提供的国家名称提供城市名称。

请帮我分析上面的JSON文件....

感谢, Yugandhar

回答

1
var info = { 
    "Name1": { 
     "id": "32313", 
     "latitude": "57.1", 
     "longitude": "9.85", 
     "timezone": "2", 
     "city": "Aalborg", 
     "country": "Denmark", 
     "country_code": "DK" 
    }, 
    .... 
    "Name8": { 
     "id": "32325", 
     "latitude": "57.2", 
     "longitude": "-2.2", 
     "timezone": "1", 
     "city": "Aberdeen", 
     "country": "United Kingdom", 
     "country_code": "GB" 
    } 
} 

//stores countries in an array 
var countries = []; 
//stores cities in an object keyed on the contrycode 
var cities = {};   
var keyNames = {}; 
for(var prop in ​info){ 
    var country = info[prop]["country"]; 
    var country_code = info[prop]["country_code"]; 
    var cityname = info[prop]["city"]; 

    countries.push(country); 
    var city = cities[country_code]; 
    if(typeof city == "undefined"){ 
     cities[country_code] = [];   
    } 

    var key = keyNames[country_code]; 
    if(typeof key == "undefined"){ 
     keyNames[country_code] = []; 
    } 

    cities[country_code].push(cityname); 
    keyNames[country_code].push(prop); 
} 

console.log(cities); 
console.log(countries); 

+0

太感谢你了......它的工作.... –

+0

高兴它制定了雅:) –

+0

如何获得基于国家或城市名称的关键名称...例如:getKeyName(国家)应该返回“Name1/Name2 /”那样 –

1
var countries = []; 
// Vanilla Javascript 
// Get Array of countries 
for(var name in json){ 
    countries.push(json[name]["country"]); 
} 
// Get city name based on country name 

var getCities = function(country){ 
    var cities = []; 
    for(var name in json){ 
    if (country === json[name]["country"]){ 
     cities.push(name); 
    } 
    } 
    return cities; 
} 

// Usage 
var cities = getCities("Denmark"); // 
0

Underscore.js(http://documentcloud.github.com/underscore/)是瑞士军刀的JavaScript库,在处理数据集时非常有用,例如在这种情况下。

我创建了一个解决您的两个用例http://jsfiddle.net/4NJPc/

这里是重要的部分一的jsfiddle:

function getCityData(country) { 
    return _(data) 
     .filter(function(el) { 
      return el.country === country; 
     });   
} 

function getCountries() { 
    return _(data).chain().pluck("country").uniq().value(); 
} 
相关问题