在使用此 JS 代碼后,我正在嘗試訪問物件中的最新元素Time Series (5 min),而無需指定日期/時間:
var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);
var current_stock = JSON.parse(getStock.responseText);
console.log(current_stock);
var current_stock_price = current_stock["Time Series (5min)"][0]["4. close"];
所以在這種情況下(見截圖)它是Time Series (5 min)> 2022-04-21 20:00:00-> 4. close,但我得到一個未定義的錯誤。
我什至在開發者控制臺中嘗試使用完整的 JSON 檔案。usingcurrent_stock["Time Series (5 min)"]回傳控制臺中的所有子值,但在末尾添加[0]or["2022-04-21 20:00:00"]會引發未定義的錯誤。

uj5u.com熱心網友回復:
您可以像這樣訪問它:
var getStock = new XMLHttpRequest();
getStock.open("GET", "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);
var current_stock = JSON.parse(getStock.responseText);
const timeSeries = current_stock['Time Series (5min)']
const key = Object.keys(timeSeries)[0]
console.log(timeSeries[key]['4. close'])
uj5u.com熱心網友回復:
那是因為 current_stock["Time Series (5min)"] 是一個物件。不是陣列,這就是 0 索引不可用的原因。如果您想訪問 current_stock["Time Series (5min)"] 上的第一項,您可以執行以下操作
var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);
var current_stock = JSON.parse(getStock.responseText);
console.log(current_stock);
var keys = Object.keys(current_stock["Time Series (5min)"]); // return all the keys in current_stock["Time Series (5min)"] object
console.log(keys) // keys is array, so you can access first item as keys[0]
var current_stock_price = current_stock["Time Series (5min)"][keys[0]]["4. close"]
uj5u.com熱心網友回復:
當我將 [0] 更改為 ["2022-04-21 20:00:00"] 時,就像您建議的那樣,它作業得很好。然后代碼將是:
var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);
var current_stock = JSON.parse(getStock.responseText);
var current_stock_price = current_stock["Time Series (5min)"]["2022-04-21 20:00:00"]["4. close"];
console.log(current_stock_price)
如果您不想使用可Object.values()用于訪問資料的密鑰:
var getStock = new XMLHttpRequest();
getStock.open("GET","https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=5min&apikey=demo", false);
getStock.send(null);
var current_stock = JSON.parse(getStock.responseText);
var current_stock_price = Object.values(current_stock["Time Series (5min)"])[0]["4. close"];
console.log(current_stock_price)
這將回傳最新條目。
uj5u.com熱心網友回復:
問題是您試圖將物件作為陣列訪問。
current_stock["Time Series (5min)"]["2022-04-21 20:00:00"]["4. close"]應該讓你的示例代碼作業。
要回答您關于如何回圈的問題,您應該將物件轉換為陣列。
一個示例是使用Object.entries(),如下所示:
stock_date_prices = Object.entries(current_stock['Time Series (5min)'])
for (const [datetime, prices] in stock_date_prices) {
console.log("The closing price at %s was %s", datetime, prices['4. close'])
}
有關如何回圈 javascript 物件的更詳細的答案,請查看此 SO answer。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463026.html
標籤:javascript json
上一篇:反序列化串列串列
