首页 > javascript > 使用javascript遍历JSON以查找键内的数组中的特定值

使用javascript遍历JSON以查找键内的数组中的特定值 (Traversing JSON with javascript to find a specific value within an array which is inside a key)

2019-03-07 javascriptjson

问题

所以我试图访问外部JSON对象中的特定值,这是这个特定的结构:

{
resource: "playerdashboardbygeneralsplits",
parameters: {},
 resultSets: [
 {
  name: "OverallPlayerDashboard",
  headers: [],
  rowSet: [
   [
   "Overall",
   "2018-19",
   60,
   37,
   23,
   0.617,
   2247.0583333333334,
   651,
   1483,
   0.439,
   294,
   809,
   0.363,
   603,
   687,
   0.878,
   55,
   334,
   389,
   451,
   314,
   130,
   45,
   91,
   196,
   452,
   2199,
   119,
   3553.3,
   23,
   6,
   1,
   1
   ]
  ]
 },
{},
{},
{},
{},
{},
{}
]
}

我可以一直显示第一个孩子rowSet给我的数组。从这里开始,我不知道如何在这个数组中返回第27个(2199)值。

我的js文件的全部内容如下:

var express = require('express')
var fetch = require('isomorphic-fetch')
var app = express()

async function nbaFetch(){
    let result = await fetch('https://stats.nba.com/stats/playerdashboardbygeneralsplits?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerID=201935&PlusMinus=N&Rank=N&Season=2018-19&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=general&VsConference=&VsDivision=', {
        mode: 'cors',
        method: "GET", // *GET, POST, PUT, DELETE, etc.
        headers: {
            
        "accept-encoding": "Accepflate, sdch",
        "accept-language": "he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4",
        "cache-control": "max-age=0",
        connection: "keep-alive",
        host: "stats.nba.com",
        referer: "http://stats.nba.com/",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
        },
    })

    let nbaFileStruct = await result.json()

    
    return nbaFileStruct
}
app.use('/', async function (req, res, next) {
    let result = await nbaFetch().catch(error => console.log(error))
    res.send(JSON.stringify(result.resultSets[0].rowSet[0]))
    
})

app.listen(3001, console.log("I'm a server and I am listening on port 3001"))


有没有一种简单的方法来访问嵌套数组中的值?我搜索了网站的其余部分,但找不到这个具体问题的答案。

任何帮助将不胜感激。

解决方法

您可以指定数组的索引。在这种情况下,数组中的第27个是索引26。

const result = {
  resource: "playerdashboardbygeneralsplits",
  parameters: {},
  resultSets: [{
      name: "OverallPlayerDashboard",
      headers: [],
      rowSet: [
        [
          "Overall",
          "2018-19",
          60,
          37,
          23,
          0.617,
          2247.0583333333334,
          651,
          1483,
          0.439,
          294,
          809,
          0.363,
          603,
          687,
          0.878,
          55,
          334,
          389,
          451,
          314,
          130,
          45,
          91,
          196,
          452,
          2199,
          119,
          3553.3,
          23,
          6,
          1,
          1
        ]
      ]
    },
    {},
    {},
    {},
    {},
    {},
    {}
  ]
}
console.log(result.resultSets[0].rowSet[0][26])


问题

So i'm trying to access a specific value within an external JSON object, which is this specific structure:

{
resource: "playerdashboardbygeneralsplits",
parameters: {},
 resultSets: [
 {
  name: "OverallPlayerDashboard",
  headers: [],
  rowSet: [
   [
   "Overall",
   "2018-19",
   60,
   37,
   23,
   0.617,
   2247.0583333333334,
   651,
   1483,
   0.439,
   294,
   809,
   0.363,
   603,
   687,
   0.878,
   55,
   334,
   389,
   451,
   314,
   130,
   45,
   91,
   196,
   452,
   2199,
   119,
   3553.3,
   23,
   6,
   1,
   1
   ]
  ]
 },
{},
{},
{},
{},
{},
{}
]
}

I can make it all the way to displaying the first child of rowSet which gives me the array within. From here, i'm not sure how to return the 27th (2199) value within this array.

Full contents of my js file below:

var express = require('express')
var fetch = require('isomorphic-fetch')
var app = express()

async function nbaFetch(){
    let result = await fetch('https://stats.nba.com/stats/playerdashboardbygeneralsplits?DateFrom=&DateTo=&GameSegment=&LastNGames=0&LeagueID=00&Location=&MeasureType=Base&Month=0&OpponentTeamID=0&Outcome=&PORound=0&PaceAdjust=N&PerMode=Totals&Period=0&PlayerID=201935&PlusMinus=N&Rank=N&Season=2018-19&SeasonSegment=&SeasonType=Regular+Season&ShotClockRange=&Split=general&VsConference=&VsDivision=', {
        mode: 'cors',
        method: "GET", // *GET, POST, PUT, DELETE, etc.
        headers: {
            
        "accept-encoding": "Accepflate, sdch",
        "accept-language": "he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4",
        "cache-control": "max-age=0",
        connection: "keep-alive",
        host: "stats.nba.com",
        referer: "http://stats.nba.com/",
        "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
        },
    })

    let nbaFileStruct = await result.json()

    
    return nbaFileStruct
}
app.use('/', async function (req, res, next) {
    let result = await nbaFetch().catch(error => console.log(error))
    res.send(JSON.stringify(result.resultSets[0].rowSet[0]))
    
})

app.listen(3001, console.log("I'm a server and I am listening on port 3001"))

Is there an easy way to access a value inside the nested array? I searched the rest of the site but couldn't find an answer to this specific problem.

Any help would be greatly appreciated.

解决方法

You can specify the index of the array. In this case, 27th in the array is the index 26.

const result = {
  resource: "playerdashboardbygeneralsplits",
  parameters: {},
  resultSets: [{
      name: "OverallPlayerDashboard",
      headers: [],
      rowSet: [
        [
          "Overall",
          "2018-19",
          60,
          37,
          23,
          0.617,
          2247.0583333333334,
          651,
          1483,
          0.439,
          294,
          809,
          0.363,
          603,
          687,
          0.878,
          55,
          334,
          389,
          451,
          314,
          130,
          45,
          91,
          196,
          452,
          2199,
          119,
          3553.3,
          23,
          6,
          1,
          1
        ]
      ]
    },
    {},
    {},
    {},
    {},
    {},
    {}
  ]
}
console.log(result.resultSets[0].rowSet[0][26])

相似信息