2015-11-21 75 views
2

我試圖從我的JSON響應訪問所需的值,以加載到下拉框中。但我得到undefined作爲下拉項目。Reguarding訪問JSON響應的值

這裏是我的JSON響應:

{"currency":[{"code":"USD","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":1,"status":true,"symbolLeft":"$","symbolRight":"","tiendayaOrderses":[],"title":"US Dollar","value":1.00000000},{"code":"EUR","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":2,"status":true,"symbolLeft":"","symbolRight":"€","tiendayaOrderses":[],"title":"Euro","value":0.76775432},{"code":"GBP","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":3,"status":true,"symbolLeft":"£","symbolRight":"","tiendayaOrderses":[],"title":"Pound Sterling","value":0.64524455}]} 

這是我如何獲取我的迴應:

$(document).ready(function() { 
    $.ajax({ 
     url: "currencies", 
     dataType: 'json', 
     error: function() { 
      alert('error'); 
     }, 
     success: function(data) { 
      //var jsonData = $.parseJSON(data); 
      $.each(data.currency, function(index) { 
       $.each(data.currency[index], function(key, value) { 
        //curncyDrop is the drop-down box 
        $('#curncyDrop').append('<li><a>' + value.title + 
         '</a></li>'); 


       }); 

      }); 
     } 

    }); 
}); 

這會將所有數據所需要的下拉,而不是裝載(標題)值:

$('#curncyDrop').append('<li><a>' + value + '</a></li>'); 

在此先感謝。

+0

什麼是嵌套的'each'報表?看起來你在響應中只有一個數組。 –

回答

2

var data = { 
 
    "currency": [{ 
 
    "code": "USD", 
 
    "dateModified": "2015-10-30T20:43:58", 
 
    "decimalPlace": "2", 
 
    "id": 1, 
 
    "status": true, 
 
    "symbolLeft": "$", 
 
    "symbolRight": "", 
 
    "tiendayaOrderses": [], 
 
    "title": "US Dollar", 
 
    "value": 1.00000000 
 
    }, { 
 
    "code": "EUR", 
 
    "dateModified": "2015-10-30T20:43:58", 
 
    "decimalPlace": "2", 
 
    "id": 2, 
 
    "status": true, 
 
    "symbolLeft": "", 
 
    "symbolRight": "€", 
 
    "tiendayaOrderses": [], 
 
    "title": "Euro", 
 
    "value": 0.76775432 
 
    }, { 
 
    "code": "GBP", 
 
    "dateModified": "2015-10-30T20:43:58", 
 
    "decimalPlace": "2", 
 
    "id": 3, 
 
    "status": true, 
 
    "symbolLeft": "£", 
 
    "symbolRight": "", 
 
    "tiendayaOrderses": [], 
 
    "title": "Pound Sterling", 
 
    "value": 0.64524455 
 
    }] 
 
} 
 

 

 

 
$.each(data, function(index, value){ 
 
$.each(value, function(indx, val){ 
 
$('#curncyDrop').append('<li><a>' + val.title + '</a></li>'); 
 
    $('#curncyDrop').append('<li><a>' + val.value + '</a></li>'); 
 
}) 
 
}) 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<ul id='curncyDrop'></ul>

嘗試這樣

+0

謝謝..它的工作.. :) –

+0

@MadushanPerera很高興它的作品 – guradio

1

我認爲你有不必要的循環對那裏發生的,只要你的數據是有效的JSON和title屬性存在,你應該能夠做到以下幾點:

$(document).ready(function() { 
    $.ajax({ 
     url: "currencies", 
     dataType: 'json', 
     error: function() { 
      alert('error'); 
     }, 
     success: function (data) { 
      $.each(data.currency, function (key, value) { 
       $('#curncyDrop').append('<li><a>' + value.title +'</a></li>'); 
      }); 
     } 
    }); 
}); 

如果它不是一個JSON對象然後使用JSON.parse(數據),如果你仍然收到未定義的錯誤,我建議使用console.log(數據)來幫助進一步調查。

+0

嘗試不嵌套'each'。得到相同的結果,但只有3個「未定義」。 :) –

+0

@MadushanPerera console.log out數據和值來檢查傳遞的是什麼。 –

1

試試這個:

var currencyData = data.currency; 
    $.each(currencyData, function(key, value){ 
     //curncyDrop is the drop-down box 
     $('#curncyDrop').append('<li><a>' + value.title +'</a></li>'); 
    }); 
+1

有沒有必要解析,因爲他指定的數據類型爲json – guradio