2013-11-29 152 views
1

我有airport.json文件。這些數據也正是如此,用jquery加載json嵌套對象

{ 
    "diagnostic":{ 
     "status":200, 
     "elapsetime":"12.9290", 
     "memoryusage":"15.39MB", 
     "confirm":"success", 
     "lang":"id", 
     "currency":"IDR" 
    }, 
    "output_type":"json", 
    "all_airport":{ 
    "airport":[ 
     { 
     "airport_name":"PATTIMURA", 
     "airport_code":"AMQ", 
     "location_name":"Ambon", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"SOA", 
     "airport_code":"BJW", 
     "location_name":"Bajawa", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"SEPINGGAN", 
     "airport_code":"BPN", 
     "location_name":"BalikPapan", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"SULTAN ISKANDAR MUDA", 
     "airport_code":"BTJ", 
     "location_name":"Banda Aceh", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"HUSEIN SASTRANEGARA", 
     "airport_code":"BDO", 
     "location_name":"Bandung", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"SYAMSUDDIN NOOR", 
     "airport_code":"BDJ", 
     "location_name":"Banjarmasin", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"BLIMBINGSARI", 
     "airport_code":"DQJ", 
     "location_name":"Banyuwangi", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"HANG NADIM", 
     "airport_code":"BTH", 
     "location_name":"Batam", 
     "country_id":"id" 
     }, 
     { 
     "airport_name":"BAUBAU", 
     "airport_code":"BUW", 
     "location_name":"Baubau", 
     "country_id":"id" 
     } 
    ] 
    } 
} 

如何獲得「機場」數據,如「AIRPORT_NAME」,「AIRPORT_CODE」,「LOCATION_NAME」等在其陣列機場all_airport對象的哪些價值?

謝謝大家,它完成了。

$.ajax({ 
    url:'airport.json', 
    dataType: 'json', 
    success: function(json) { 
     // get the `airport` array 
     var airports= json.all_airport.airport; 

     // loop through the array to populate your list 
     $.each(airports, function(i, currentAirport) { 
     // add and option tag to your existing list 
     $('#yourlist').append(new Option(currentAirport.airport_name)); 
     }); 
    } 
    }); 
+0

您正在使用哪種語言?任何代碼嘗試? – Raptor

+0

我'使用JQuery – Beye

+2

你的代碼在哪裏?如果你在Stack Overflow/Google中搜索,你會發現很多使用jQuery解析JSON的例子。 – Raptor

回答

2

您的成功,函數應該看看像這樣:

success: function(json){ 
    // get the `airport` array 
    var airports= json.all_airport.airport; 

    // loop through the array to populate your list 
    $.each(airports, function(i, currentAirport) { 
    // add and option tag to your existing list 
    $('#yourlist').append(new Option(currentAirport.airport_name)); 
    }); 
} 

看完這個答案後你會更好的理解它:Access/process (nested) objects, arrays or JSON

但是在編寫代碼後肯定會得到它。您可以直接在瀏覽器的JavaScript控制檯上編寫,編寫,測試,修復和重複該過程,直到獲得所需的代碼。這總能幫助我更好地理解我的代碼。

+0

非常感謝。我只是混淆瞭如何捕捉json嵌套對象。作爲xml文件來閱讀並不容易。我想,我會更多地瞭解這一點。再一次,謝謝。 – Beye

+0

有趣的是,你應該說,很多人認爲這是相反的,也許只是習慣於JSON格式的問題,在這方面它比XML有一些優勢。以下是XML如何映射到JSON等效項的一些示例:http://json.org/example –

0

你不需要jQuery的使用JSON.parse這樣的:

JSON.parse('{"diagnostic":{"status":200,"elapsetime":"12.9290","memoryusage":"15.39MB","confirm":"success","lang":"id","currency":"IDR"},"output_type":"json","all_airport":{"airport":[{"airport_name":"PATTIMURA","airport_code":"AMQ","location_name":"Ambon","country_id":"id"},{"airport_name":"SOA","airport_code":"BJW","location_name":"Bajawa","country_id":"id"},{"airport_name":"SEPINGGAN","airport_code":"BPN","location_name":"BalikPapan","country_id":"id"},{"airport_name":"SULTAN ISKANDAR MUDA","airport_code":"BTJ","location_name":"Banda Aceh","country_id":"id"},{"airport_name":"HUSEIN SASTRANEGARA","airport_code":"BDO","location_name":"Bandung","country_id":"id"},{"airport_name":"SYAMSUDDIN NOOR","airport_code":"BDJ","location_name":"Banjarmasin","country_id":"id"},{"airport_name":"BLIMBINGSARI","airport_code":"DQJ","location_name":"Banyuwangi","country_id":"id"},{"airport_name":"HANG NADIM","airport_code":"BTH","location_name":"Batam","country_id":"id"},{"airport_name":"BAUBAU","airport_code":"BUW","location_name":"Baubau","country_id":"id"}]}}'); 
+0

* .json文件怎麼樣?它可以像這樣「JSON.parse(*。json)」? – Beye

+0

@ user2349609:否,'JSON.parse'需要包含JSON的字符串。你必須先加載數據。由於您使用的是jQuery,請閱讀關於'$ .getJSON'的文檔。它包含你必須知道的一切。 http://api.jquery.com/jQuery.getJSON/ –

+0

@FelixKling:我已閱讀該文檔。但它仍然行不通。我只是加載它,然後在「成功」步驟中,我無法在html elemnt中顯示它。 – Beye

0

JavaScript有內置JSON解析器:

var jsonObject = JSON.parse(json_string) 

但是它的jQuery的版本更好,因爲它使用JSON.parse(如果可用),或者如果不可用,則創建它自己的解析器(基本術語中的jQuery哲學):

var jsonObject = jQuery.parseJSON(json_string) 

的更多信息:http://api.jquery.com/jQuery.parseJSON/

0

如果你使用jQuery請求通過AJAX的JSON文件,只添加一個參數:

dataType = json 

這樣的:

$.ajax({ 
    url: "your request url", 
    dataType: "json", 
    data:{ 
     "your parameters of url" 
    }, 
    success: function(responseData){ 
     //responseData has been parsed to a object 
     console.log(responseData.foo); 
    } 
}) 
+0

什麼是「你的url參數」看起來像基於我的airport.json文件? – Beye

+0

@ user2349609:您可能可以省略該設置。 –