我如何解析这个复杂的json nodejs

时间:2018-11-30 12:01:31

标签: javascript node.js json

我正在尝试解析json

    request(requestOptions,(err,res,body)=>{

      for(var i in body){
        console.log(body.data[0].quote.USD.price)
      }

  })

而json就像

{ status: {something},
  data: [{
          id:1,
     name:'Bitcoin',
     symbol:'BTC',
     slug:'bitcoin',
     circulating_supply:17400325,
     total_supply:17400325,
     max_supply:21000000,
     date_added:'2013-04-28T00:00:00.000Z',
     num_market_pairs:6618,
     tags:[
        Array
     ],
     cmc_rank:1,
     last_updated:'2018-11-30T11:00:28.000Z',
     quote:[
        Object
     ]
        }]
}

引用对象为:

{ USD:
{ price: 4071.55349237,
 volume_24h: 5966638087.9657,
 percent_change_1h: -3.18515,
 percent_change_24h: -6.66359,
 percent_change_7d: -5.74611,
 market_cap: 70846354022.12302,
 last_updated: '2018-11-30T11:03:22.000Z' } }

“我提到了两个json对象,另一个是上述json的副本”。

{ status:{ },
   data:[ ]
}

我在上面的代码中运行,它给出了以下输出:

1 比特币 比特币 4030.51947765 1个 比特币 比特币 4030.51947765

请帮助我如何获取对象的所有“ id”,“名称”,“符号”,“价格”,并将其保存到一个数组中。

2 个答案:

答案 0 :(得分:0)

只需将其映射:

https://stackblitz.com/edit/js-c1fwsf

const req = { 
  data: [
    {
     id:1,
     name:'Bitcoin',
     symbol:'BTC',
     slug:'bitcoin',
     circulating_supply:17400325,
     total_supply:17400325,
     max_supply:21000000,
     date_added:'2013-04-28T00:00:00.000Z',
     num_market_pairs:6618,
     cmc_rank:1,
     last_updated:'2018-11-30T11:00:28.000Z',
     quote:[
        { 
          USD:
          { 
            price: 4071.55349237,
            volume_24h: 5966638087.9657,
            percent_change_1h: -3.18515,
            percent_change_24h: -6.66359,
            percent_change_7d: -5.74611,
            market_cap: 70846354022.12302,
            last_updated: '2018-11-30T11:03:22.000Z' 
          } 
        }
     ],
    },
     {
     id:2,
     name:'Litecoin',
     symbol:'LTC',
     slug:'litecoin',
     circulating_supply:17400325,
     total_supply:17400325,
     max_supply:21000000,
     date_added:'2013-04-28T00:00:00.000Z',
     num_market_pairs:6618,
     cmc_rank:1,
     last_updated:'2018-11-30T11:00:28.000Z',
     quote:[
        { 
  USD:
  { 
    price: 221.55349237,
    volume_24h: 5966638087.9657,
    percent_change_1h: -3.18515,
    percent_change_24h: -6.66359,
    percent_change_7d: -5.74611,
    market_cap: 70846354022.12302,
    last_updated: '2018-11-30T11:03:22.000Z' 
  } 
}
     ]
  }]
}

const res = req.data.map(x => ({
  name: x.name,
  id: x.id,
  symbol: x.symbol,
  price: x.quote[0].USD.price,
}))

console.log(res)

See!

答案 1 :(得分:0)

尝试这个

let mydata = { status: "something",
      data: [{
              id:1,
         name:'Bitcoin',
         symbol:'BTC',
         slug:'bitcoin',
         circulating_supply:17400325,
         total_supply:17400325,
         max_supply:21000000,
         date_added:'2013-04-28T00:00:00.000Z',
         num_market_pairs:6618,
         tags:[
            "Something"
         ],
         cmc_rank:1,
         last_updated:'2018-11-30T11:00:28.000Z',
         quote:
            { USD:
                { price: 4071.55349237,
                 volume_24h: 5966638087.9657,
                 percent_change_1h: -3.18515,
                 percent_change_24h: -6.66359,
                 percent_change_7d: -5.74611,
                 market_cap: 70846354022.12302,
                 last_updated: '2018-11-30T11:03:22.000Z' } }

            }]
    }
let myArray = []
mydata.data.map((o)=>{
    const { id, name, symbol } = o
    myArray.push({
        id,
        name,
        symbol,
        price : o.quote.USD.price
    })
})
console.log(myArray)