无法从 API 获取值
Cannot get a value from API
我对来自 polygon.io 的 API 呼叫有疑问。
我正在使用他们的免费 API,它为我提供了有关股票代码的信息。
我正在使用一个 API,它会在我被调用时提供响应。
从响应中,我需要得到响应>数据>结果>“c”值,在这种情况下,1014.97
到目前为止我能做的最好的是:
console.log(response.data.results)
给出:
Array [
Object {
"T": "TSLA",
"c": 1014.97,
"h": 1090.5753,
"l": 1000.21,
"n": 1043576,
"o": 1084.79,
"t": 1638565200000,
"v": 30773995,
"vw": 1028.7486,
},
请告诉我如何获得“c”值?
这是完整的回复数据:
console.log(response)
这个,在 VS-Code>Terminal 中显示:
Object {
"config": Object {
"adapter": [Function xhrAdapter],
"baseURL": "https://api.polygon.io/",
"data": undefined,
"headers": Object {
"Accept": "application/json",
},
"maxBodyLength": -1,
"maxContentLength": -1,
"method": "get",
"params": Object {
"apiKey": "Gp5IgTvT4fPbfhINPpOk7iBIUDjOTRc9",
},
"timeout": 5000,
"transformRequest": Array [
[Function transformRequest],
],
"transformResponse": Array [
[Function transformResponse],
],
"transitional": Object {
"clarifyTimeoutError": false,
"forcedJSONParsing": true,
"silentJSONParsing": true,
},
"url": "v2/aggs/ticker/MSFT/prev",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": Object {
"adjusted": true,
"count": 1,
"queryCount": 1,
"request_id": "6a2a6c28efc5fcf9e4b1f295f845bf91",
"results": Array [
Object {
"T": "TSLA",
"c": 1014.97,
"h": 1090.5753,
"l": 1000.21,
"n": 1043576,
"o": 1084.79,
"t": 1638565200000,
"v": 30773995,
"vw": 1028.7486,
},
],
"resultsCount": 1,
"status": "OK",
"ticker": "MSFT",
},
"duration": 406,
"headers": Object {
"content-type": "application/json",
"date": "Sat, 04 Dec 2021 21:37:55 GMT",
"server": "nginx/1.19.2",
"strict-transport-security": "max-age=15724800; includeSubDomains",
"vary": "Accept-Encoding",
"x-request-id": "6a2a6c28efc5fcf9e4b1f295f845bf91",
},
"ok": true,
"originalError": null,
"problem": null,
"status": 200,
}
由于“results”是一个包含字典的数组,您可以使用 response.data.results['c'].
引用收盘值
结果是一个字典对象数组。包含“c”值的字典对象是数组的第一个元素。
所以,你可以这样得到它
response.data.results[0].c
我对来自 polygon.io 的 API 呼叫有疑问。 我正在使用他们的免费 API,它为我提供了有关股票代码的信息。
我正在使用一个 API,它会在我被调用时提供响应。 从响应中,我需要得到响应>数据>结果>“c”值,在这种情况下,1014.97
到目前为止我能做的最好的是:
console.log(response.data.results)
给出:
Array [
Object {
"T": "TSLA",
"c": 1014.97,
"h": 1090.5753,
"l": 1000.21,
"n": 1043576,
"o": 1084.79,
"t": 1638565200000,
"v": 30773995,
"vw": 1028.7486,
},
请告诉我如何获得“c”值?
这是完整的回复数据:
console.log(response)
这个,在 VS-Code>Terminal 中显示:
Object {
"config": Object {
"adapter": [Function xhrAdapter],
"baseURL": "https://api.polygon.io/",
"data": undefined,
"headers": Object {
"Accept": "application/json",
},
"maxBodyLength": -1,
"maxContentLength": -1,
"method": "get",
"params": Object {
"apiKey": "Gp5IgTvT4fPbfhINPpOk7iBIUDjOTRc9",
},
"timeout": 5000,
"transformRequest": Array [
[Function transformRequest],
],
"transformResponse": Array [
[Function transformResponse],
],
"transitional": Object {
"clarifyTimeoutError": false,
"forcedJSONParsing": true,
"silentJSONParsing": true,
},
"url": "v2/aggs/ticker/MSFT/prev",
"validateStatus": [Function validateStatus],
"xsrfCookieName": "XSRF-TOKEN",
"xsrfHeaderName": "X-XSRF-TOKEN",
},
"data": Object {
"adjusted": true,
"count": 1,
"queryCount": 1,
"request_id": "6a2a6c28efc5fcf9e4b1f295f845bf91",
"results": Array [
Object {
"T": "TSLA",
"c": 1014.97,
"h": 1090.5753,
"l": 1000.21,
"n": 1043576,
"o": 1084.79,
"t": 1638565200000,
"v": 30773995,
"vw": 1028.7486,
},
],
"resultsCount": 1,
"status": "OK",
"ticker": "MSFT",
},
"duration": 406,
"headers": Object {
"content-type": "application/json",
"date": "Sat, 04 Dec 2021 21:37:55 GMT",
"server": "nginx/1.19.2",
"strict-transport-security": "max-age=15724800; includeSubDomains",
"vary": "Accept-Encoding",
"x-request-id": "6a2a6c28efc5fcf9e4b1f295f845bf91",
},
"ok": true,
"originalError": null,
"problem": null,
"status": 200,
}
由于“results”是一个包含字典的数组,您可以使用 response.data.results['c'].
引用收盘值结果是一个字典对象数组。包含“c”值的字典对象是数组的第一个元素。 所以,你可以这样得到它
response.data.results[0].c