[英]Trouble with accessing object in JSON array
我似乎無法弄清楚如何使此代碼片段正常工作。 我正在嘗試訪問此json代碼段中的“名稱”對象。 任何幫助,將不勝感激。
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"> </script> <script> $(document).ready(function () { $.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function(data) { $('#demo').text(data[0].Name); }); }); </script> </head> <body> <p id="demo"></p> </body> </html>
用這個:
$(document).ready(function () {
$.getJSON('https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=', function(data) {
$('#demo').text(data.query.results.quote.Name);
});
});
{
"query": {
"count": 1,
"created": "2017-02-13T18:34:48Z",
"lang": "es-419",
"results": {
"quote": {
"name": "Blabla"
所以你有data.query.results.quote.name
您的API調用不返回數組,而是返回JSON對象。
試試: $('#demo').text(data.query.results.quote.Name);
返回的數據結構如下所示:
{
"query": {
"count": 1,
"created": "2017-02-13T18:34:12Z",
"lang": "en-us",
"results": {
"quote": {
// other props...
"Name": "Apple Inc.",
// other props...
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.