【问题标题】:Trying to acceess data from fetched result from RapidAPI尝试从 RapidAPI 的获取结果中访问数据
【发布时间】:2021-02-17 19:47:27
【问题描述】:

我是网络编程的新手,我需要帮助。

我正在使用 Node.js 通过 RapidAPI 获取数据 获取的结果以数组中的 Parsed JSON 格式返回给我。 但是,如果我要给出一个索引,它会返回字母而不是我想查看的项目。

以下是我必须获取 Apple 结果的代码:

const express = require('express');
const bodyParser = require('body-parser');
const http = require("https");

const app = express();
app.use(bodyParser.urlencoded({extended:true}));

app.get("/", function(request, response){
    response.sendFile(__dirname + "/index.html");
});

app.post("/", function(request, response){
const options = {
    "method": "get",
    "hostname": "rapidapi.p.rapidapi.com",
    "port": null,
    "path": "/income-statement/AAPL?apikey=demo",
    "headers": {
        "x-rapidapi-key": "895157e459mshecb81dbe427f124p1fe70cjsn772a488898eb",
        "x-rapidapi-host": "financial-modeling-prep.p.rapidapi.com",
        "useQueryString": true
    }
};

const req = http.request(options, function (res) {
    const chunks = [];
    
    if (res.statusCode === 200) {
        console.log("Success");
    } else {
        console.log("Fail");
    }

    res.on("data", function (chunk) {
        console.log(chunk.toString('utf-8')[23]);
        chunks.push(chunk);
    });

    res.on("end", function () {
        const body = Buffer.concat(chunks); 

    });
});    
req.end();
});

“块”的日志结果:
[38 项
0:{46 项
“日期”:“2020-09-26”
“符号”:“AAPL”
"fillingDate":"2020-10-30"
"acceptedDate":"2020-10-29 18:06:25"
"期":"FY"
"cashAndCashEquivalents":38016000000
“短期投资”:52927000000
"cashAndShortTermInvestments":90943000000
“净应收账款”:16120000000
“库存”:4061000000
“其他当前资产”:32589000000
“总当前资产”:143713000000
"propertyPlantEquipmentNet":36766000000
“善意”:0
“无形资产”:0
"goodwillAndIntangibleAssets":0
“长期投资”:100887000000
"taxAssets":0
“其他非当前资产”:42522000000
“总非当前资产”:180175000000
“其他资产”:90482000000
“总资产”:323888000000
“应付账款”:42296000000
“短期债务”:8773000000
"应纳税款":0
"deferredRevenue":6643000000
"otherCurrentLiabilities":47680000000
"totalCurrentLiabilities":105392000000
“长期债务”:98667000000
"deferredRevenueNonCurrent":0
"deferredTaxLiabilitiesNonCurrent":0
"otherNonCurrentLiabilities":54490000000
"totalNonCurrentLiabilities":153157000000
“其他负债”:0
“总负债”:258549000000
"commonStock":16976763000
“留存收益”:14966000000
“累计其他综合收入损失”:-406000000
"othertotalStockholdersEquity":33802237000
"totalStockholdersEquity":65339000000
"totalLiabilitiesAndStockholdersEquity":323888000000
“总投资”:153814000000
“总债务”:107440000000
“净债务”:69424000000
“链接”:“https://www.sec.gov/Archives/edgar/data/320193/000032019320000096/0000320193-20-000096-index.htm”
“finalLink”:“https://www.sec.gov/Archives/edgar/data/320193/000032019320000096/aapl-20200926.htm”
},...]

问题:如果我特别想从响应中访问特定字段,例如“netDebt”,我将如何访问它?

ex) chunck[0] 返回一个类似“l”的字母。我想我不清楚它的结构。 我在想类似 chunk[0]["netDebt"]

谢谢,

【问题讨论】:

    标签: node.js json api web-project rapidapi


    【解决方案1】:

    响应是对象数组。如果要从对象数组中访问特定的键值对,可以通过以下方式进行。

    const arr = [
        {
            "name": "Pratham",
            "age": 22
        }, 
        {
            "twitter": "prathkum",
            "followers": 116000
        }
    ];
    
    console.log(arr[0].name); // Pratham
    console.log(arr[1].followers); // 116000
    

    所以如果你想从chunk数组中访问netDebt键,你可以这样做。

    chunk[0].netDebt // 69424000000
    

    附:您刚刚在问题中发布的代码 sn-p 中公开了您的 API 密钥。 API 密钥是敏感数据,现在可以公开访问。我建议您删除此密钥并为您生成一个新密钥。您可以从Developer Dashboard 中删除并生成新的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2019-08-15
      • 2022-11-23
      相关资源
      最近更新 更多