遍历api对象

时间:2019-11-24 17:03:43

标签: api buffer

让http = require(“ https”);
函数apiCall(){
        让options = {         “ method”:“ GET”,         “主机名”:“ matchilling-chuck-norris-jokes-v1.p.rapidapi.com”,
        “ port”:null,
        “ path”:“ /笑话/随机”,
        “标题”:{             “ x-rapidapi-host”:“ matchilling-chuck-norris-jokes-v1.p.rapidapi.com”,
            “ x-rapidapi-key”:“ 97ec6a9bb8msh5ce14321f472333p1cabeejsne7a32466751b”,
            “ accept”:“ application / json”
        }
    };
    let req = http.request(options,function(res){
        让块= [];
        res.on(“ data”,函数(块){
            chunks.push(chunk);
        });
        res.on(“ end”,function(){
            let body = Buffer.concat(chunks);
            让str = body.toString()
            console.log(str);         });
    });
    req.end();
}

module.exports.apiCall = apiCall

问题:当我尝试遍历str变量时,它返回一堆数字。但是console.log(str)返回一个正常的对象。我猜想既然从API派生的对象,您必须以不同的方式处理它?谢谢

0 个答案:

没有答案