2014-12-02 86 views
0

我想通过使用ISO(cca2)代码搜索一个国家的首都。在JSON文件中搜索。

我做了以下内容:JSON文件的

var json = $.getJSON('Countries.json', function(data) 
{ 
     return data; 
}); 

var CapitalCity = getCapitalByCCA2(json, code.toString().toUpperCase()); 

alert(CapitalyCity); 

function getCapitalByCCA2(json, code) { 
    for (var i = 0; i < json.length; i++) { 
       if (json[i].cca2 === code.toString().toUpperCase()) { 
        return json[i].capital; 
        console.log(json[i]); 
       } 
      } 
    return null; 
}; 

结构:

[ 
    { 
     "name": { 
      "common": "Afghanistan", 
      "official": "Islamic Republic of Afghanistan", 
      "native": { 
       "common": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646", 
       "official": "\u062f \u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646 \u0627\u0633\u0644\u0627\u0645\u064a \u062c\u0645\u0647\u0648\u0631\u06cc\u062a" 
      } 
     }, 
     "tld": [".af"], 
     "cca2": "AF", 
     "ccn3": "004", 
     "cca3": "AFG", 
     "currency": ["AFN"], 
     "callingCode": ["93"], 
     "capital": "Kabul", 
     "altSpellings": ["AF", "Af\u0121\u0101nist\u0101n"], 
     "relevance": "0", 
     "region": "Asia", 
     "subregion": "Southern Asia", 
     "nativeLanguage": "pus", 
     "languages": { 
      "prs": "Dari", 
      "pus": "Pashto", 
      "tuk": "Turkmen" 
     }, 
     "translations": { 
      "cym": "Affganistan", 
      "deu": "Afghanistan", 
      "fra": "Afghanistan", 
      "hrv": "Afganistan", 
      "ita": "Afghanistan", 
      "jpn": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3", 
      "nld": "Afghanistan", 
      "rus": "\u0410\u0444\u0433\u0430\u043d\u0438\u0441\u0442\u0430\u043d", 
      "spa": "Afganist\u00e1n" 
     }, 
     "latlng": [33, 65], 
     "demonym": "Afghan", 
     "borders": ["IRN", "PAK", "TKM", "UZB", "TJK", "CHN"], 
     "area": 652230 
    }, 
    { 
     "name": { 
      "common": "\u00c5land Islands", 
      "official": "\u00c5land Islands", 
      "native": { 
       "common": "\u00c5land", 
       "official": "Landskapet \u00c5land" 
      } 
     }, 
     "tld": [".ax"], 
     "cca2": "AX", 
     "ccn3": "248", 
     "cca3": "ALA", 
     "currency": ["EUR"], 
     "callingCode": ["358"], 
     "capital": "Mariehamn", 
     "altSpellings": ["AX", "Aaland", "Aland", "Ahvenanmaa"], 
     "relevance": "0", 
     "region": "Europe", 
     "subregion": "Northern Europe", 
     "nativeLanguage": "swe", 
     "languages": { 
      "swe": "Swedish" 
     }, 
     "translations": { 
      "deu": "\u00c5land", 
      "fra": "\u00c5land", 
      "hrv": "\u00c5landski otoci", 
      "ita": "Isole Aland", 
      "jpn": "\u30aa\u30fc\u30e9\u30f3\u30c9\u8af8\u5cf6", 
      "nld": "\u00c5landeilanden", 
      "rus": "\u0410\u043b\u0430\u043d\u0434\u0441\u043a\u0438\u0435 \u043e\u0441\u0442\u0440\u043e\u0432\u0430", 
      "spa": "Alandia" 
     }, 
     "latlng": [60.116667, 19.9], 
     "demonym": "\u00c5landish", 
     "borders": [], 
     "area": 1580 
    } 

    } 

] 

的问题是,该数据没有得到牵强。

我在做什么错?

+0

您不能从异步Ajax请求返回! – epascarello 2014-12-02 16:48:40

+0

阅读此:http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call为您的问题的第一部分得到答案。 – epascarello 2014-12-02 16:50:10

+0

你的JSON无效(最后一个额外的)。使用[JSONLint](http://jsonlint.com/)来验证这一点。 – 2014-12-02 16:50:45

回答

0

$.getJSON()不能正常工作,因为您期待它将数据返回到变量中。相反,您需要做的是提供一个$.getJSON()的回调来处理结果。尝试从成功回调中调用你的函数。

// variable to store captial city 
var CapitalCity; 
// The country code you are filtering on 
var countryCode = '...'; 
$.getJSON('Countries.json', function(data) { 
    CapitalCity = getCapitalByCCA2(data, countryCode);  
}); 

你实际上可以看看@MathieuLabrieParent提供的搜索建议,完全摆脱你的功能。然而,我可能会更改为使用$.each()进行数组迭代,以便在找到匹配项时可以跳出循环。看起来像这样:

// variable to store captial city 
var CapitalCity; 
// The country code you are filtering on 
var countryCode = code.toString().toUpperCase(); 
$.getJSON('Countries.json', function(data) { 
    $.each(data, function (index, element) { 
     if (element.cca2 === countryCode) { 
      CapitalCity = match.capital; 
      // break the loop 
      return false; 
     } 
    }); 
}); 
+0

@布莱恩看到我的答案更新后,你接受了它。它有更好的循环逻辑,这样就可以在匹配条件下打破循环。 – 2014-12-02 17:05:40

1

您应该使用grep过滤JSON像这样:

var returnedData = $.grep(json, function (element, index) { 
    return element.cca2 === code.toString().toUpperCase(); 
}); 

returnData将包含过滤JSON的数组。

我的代码中没有看到var code的任何定义。

+0

问题是与Ajax调用...你错过了。 – epascarello 2014-12-02 16:49:12

+0

糟糕...你的权利。用grep过滤仍然是一个好主意。 – 2014-12-02 16:54:26