我是JSON
和JavaScrip
t的新手。我正在弄清楚如何访问我从JSON
api收到的Alpha Vantage
输出中“ 4.关闭”中的值。我在使用多级JSON
输出中的间隔键时遇到问题。
JSON结果:
{
"Time Series (1min)": {
"2018-01-01 05:00:00": {
"1. open": "10437.3000",
"2. high": "10438.6500",
"3. low": "10431.5000",
"4. close": "10434.8000",
"5. volume": "0"
}
}
}
显示数据的相应脚本:
<script>
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(xhttp.responseText);
document.getElementById("demo").innerHTML=response["Time Series (1min)"][0]["2018-01-01 05:00:00"]["4. close"];
//to display data in <div id="demo"></div>
}
};
xhttp.open("GET", "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=NIFTY&interval=1min&outputsize=compact&apikey=NBRUZVG7R6SP321P", true);
xhttp.send();
</script>
答案 0 :(得分:1)
Time Series (1min)
不是数组,请删除[0]
document.getElementById("demo").innerHTML=response["Time Series (1min)"]["2018-01-01 05:00:00"]["4. close"];
答案 1 :(得分:0)
JSON.parse
accepts reviver function可用于过滤/编辑/查找键值对:
Public Function CellReflist(Optional r As Range) ' single cell
Dim result As Object: Dim testExpression As String: Dim objRegEx As Object
If r Is Nothing Then Set r = ActiveCell ' Cells(1, 2) ' INPUT THE CELL HERE , e.g. RANGE("A1")
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.IgnoreCase = True: objRegEx.Global = True: objRegEx.Pattern = """.*?""" ' remove expressions
testExpression = CStr(r.Formula)
testExpression = objRegEx.Replace(testExpression, "")
'objRegEx.Pattern = "(([A-Z])+(\d)+)" 'grab the address
objRegEx.Pattern = "(['\[].*?['!])?([[A-Z0-9_]+[!])?(\$?[A-Z]+\$?(\d)+(:\$?[A-Z]+\$?(\d)+)?|\$?[A-Z]+:\$?[A-Z]+|(\$?[A-Z]+\$?(\d)+))"
If objRegEx.Test(testExpression) Then
Set result = objRegEx.Execute(testExpression)
If result.Count > 0 Then CellReflist = result(0).Value
If result.Count > 1 Then
For i = 1 To result.Count - 1 'Each Match In result
dbl = False ' poistetaan tuplaesiintymiset
For j = 0 To i - 1
If result(i).Value = result(j).Value Then dbl = True
Next j
If Not dbl Then CellReflist = CellReflist & "," & result(i).Value 'Match.Value
Next i 'Match
End If
End If