首页 > 解决方案 > 尝试从 RapidAPI 获取的结果中访问数据

问题描述

我对网络编程很陌生,我需要帮助。

我正在使用 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”“ cash andCashEquilents”::38016000000
“短期估计”:52927000000
“ CASHANDSHORTTERMINEVESTMENTS”:90943000000
“ NETRECEIVABLES”:NETRECEEVABLES“:16120000000”:40611000000000000000000000000000000000000000000000000 :406110000
”:406111000000000 0000年 ”

:143713000000
“propertyPlantEquipmentNet”:36766000000
“商誉”:0
“intangibleAssets":0
"goodwillAndIntangibleAssets":0
"longTermInvestments":100887000000
"taxAssets":0
"otherNonCurrentAssets":42522000000
"totalNonCurrentAssets":180175000000
"otherAssets":90482000000
"totalAssets":323888000000
"accountPayables":42296000000
"shortTermDebt":8773000000
"taxPayables":0
"deferredRevenue":6643000000
"otherCurrentLiabilities":47680000000
"totalCurrentLiabilities":105392000000
“longTermDebt”:98667000000
“deferredRevenueNonCurrent”:0
“deferredTaxLiabilitiesNonCurrent”:0
“otherNonCurrentLiabilities”:54490000000
“totalNonCurrentLiabilities”:153157000000
“otherLiabilities”:0
“totalLiabilities”:258549000000
“commonStock”:16976763000
“retainedEarnings”:14966000000
"accumulatedOtherComprehensiveIncomeLoss":-406000000
"othertotalStockholdersEquity":33802237000
"totalStockholdersEquity":65339000000
"totalLiabilitiesAndStockholdersEquity":323888000000 "totalInvestments":153814000000
"totalDebt":107440000000
"
netDebt":69424000000
"link":"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”,我将如何访问它?

例如)chunck[0] 返回一个像“l”这样的字母。我想我不清楚它的结构。我在想像 chunk[0]["netDebt"]

谢谢,

标签: node.jsjsonapiweb-projectrapidapi

解决方案


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

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

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


推荐阅读