我如何获得特定的 JSON 项?
How do I get specific JSON item?
我正在尝试通读这些数据,但我被困在一些非常非常基本的事情上。似乎无法找到自己的方法。让我知道是否有人可以提供帮助。
代码:
const api = require('indian-stock-exchange');
const nse = api.NSE
const bse = api.BSE
nse.getIndices().then(
function(response) {
var data = response.data
console.log(data)
}
)
输出:
{
data: [
{
timeVal: 'Nov 27, 2020 15:33:24',
indexName: 'NIFTY 50',
previousClose: '12,987.00',
open: '13,012.05',
high: '13,035.30',
low: '12,914.30',
last: '12,968.95',
percChange: '-0.14',
yearHigh: '13,145.85',
yearLow: '7,511.10',
indexOrder: '0.00'
},
{
timeVal: 'Nov 27, 2020 15:33:24',
indexName: 'NIFTY NEXT 50',
previousClose: '30,186.05',
open: '30,316.45',
high: '30,591.00',
low: '30,121.35',
last: '30,334.40',
percChange: '0.49',
yearHigh: '30,591.00',
yearLow: '18,384.05',
indexOrder: '1.00'
},
{
timeVal: 'Nov 27, 2020 15:33:24',
indexName: 'NIFTY IT',
previousClose: '21,859.90',
open: '21,879.15',
high: '21,972.95',
low: '21,701.30',
last: '21,764.90',
percChange: '-0.43',
yearHigh: '22,619.00',
yearLow: '10,991.25',
indexOrder: '2.00'
}
]
}
我只想获得 Nifty50(第一个数组)结果
var firstObject = data['data'][0];
会给你数组的第一个元素。然后你可以像这样访问值:
var time = firstObject['timeVal']
或:
var time = firstObject.timeVal
我正在尝试通读这些数据,但我被困在一些非常非常基本的事情上。似乎无法找到自己的方法。让我知道是否有人可以提供帮助。
代码:
const api = require('indian-stock-exchange');
const nse = api.NSE
const bse = api.BSE
nse.getIndices().then(
function(response) {
var data = response.data
console.log(data)
}
)
输出:
{
data: [
{
timeVal: 'Nov 27, 2020 15:33:24',
indexName: 'NIFTY 50',
previousClose: '12,987.00',
open: '13,012.05',
high: '13,035.30',
low: '12,914.30',
last: '12,968.95',
percChange: '-0.14',
yearHigh: '13,145.85',
yearLow: '7,511.10',
indexOrder: '0.00'
},
{
timeVal: 'Nov 27, 2020 15:33:24',
indexName: 'NIFTY NEXT 50',
previousClose: '30,186.05',
open: '30,316.45',
high: '30,591.00',
low: '30,121.35',
last: '30,334.40',
percChange: '0.49',
yearHigh: '30,591.00',
yearLow: '18,384.05',
indexOrder: '1.00'
},
{
timeVal: 'Nov 27, 2020 15:33:24',
indexName: 'NIFTY IT',
previousClose: '21,859.90',
open: '21,879.15',
high: '21,972.95',
low: '21,701.30',
last: '21,764.90',
percChange: '-0.43',
yearHigh: '22,619.00',
yearLow: '10,991.25',
indexOrder: '2.00'
}
]
}
我只想获得 Nifty50(第一个数组)结果
var firstObject = data['data'][0];
会给你数组的第一个元素。然后你可以像这样访问值:
var time = firstObject['timeVal']
或:
var time = firstObject.timeVal