【问题标题】:Catching part of a JSON response and saving into a variable?捕获部分 JSON 响应并保存到变量中?
【发布时间】:2020-08-12 23:09:31
【问题描述】:

我有一个问题,过去几天我和我的朋友 google 一起试图回答。这是我目前正在处理的一个项目的代码,我正在尝试与两个 API 交互。

您在此处看到的是使用 GOT 库格式调用第一个 API 以接收 JSON 响应。

    var products

    //Printify call for products list
    (async () => {
        try{
            const list = await redd('shops/shopId/products.json');


            //Catch the Data array and save it into the variable products
            var obj = new JSONObject(response);
            products = obj.getJSONArray("data");


        }

        catch(error) {

        }


    })();

    //Print the variable products to the console
    console.log(products)

我从响应中创建一个新的 JSONObject 并从该响应中获取数据数组并将其放入在此函数之外定义的变量产品中。最后我试图将变量打印到控制台。

最终,我需要获取该“数据”数组并解析其中的特定项目(即title: , description: , images:)并将其作为值传递给下一个 API。

目前我从控制台收到“未定义”响应。不知道我做错了什么,希望我能得到一些帮助或指导。任何事情都非常感谢,提前谢谢大家!

【问题讨论】:

  • 什么是 JSONObject?

标签: javascript node.js json rest


【解决方案1】:

我终于能够让这一切正常工作。自我解决以来实际上已经过了一分钟,但想确保它有效。这是最终为我工作的最终代码:

//Config for a new GOT instance, to use redd as the variable with the attached headers and options
const redd = got.extend({
    prefixUrl: 'https://api.printify.com/v1',
      responseType: 'json',
      headers: {
          'Authorization': 'Bearer ' + apiKey 
      }
});

var productsParsed //Setting up global variable to accept the array from the JSON response body

//Printify call for products list
getProducts = async () => {
    try{
        const response = await redd('shops/' + shopId + '/products.json');
        productsParsed = response.body.data; //Data is the array in the body I need access to, and saving that into the var productsParsed

        //Returning the var with the new value
        return productsParsed

    }
    catch(error) {
        console.log(error.response);
    }
};

getProducts().then(console.log); //Printing the return value from getProducts() which verifies the var contains the needed value

所以解决方案最终变得相当简单。我只是没有完全理解 GOT 的结构。我不得不使用点符号来确定我的退货声明。在弄清楚这一点后,我可以使用我设置的全局变量来接受 JSON 响应正文的值。谢谢大家的建议和帮助。我希望这篇文章能够帮助任何与我有类似情况的人。

【讨论】:

    【解决方案2】:

    因为它是一个异步函数,所以在异步函数完成之前调用了 console.log。将它移到异步函数中,它应该可以工作。

    var products
    
        //Printify call for products list
        (async () => {
            try{
                const list = await redd('shops/shopId/products.json'); // we wait for the response here
    
    
                //Catch the Data array and save it into the variable products
                var obj = new JSONObject(response);
                products = obj.getJSONArray("data");
    
                //Print the variable products to the console will work here
                console.log(products)
            }
    
            catch(error) {
    
            }
        })();
    
    

    【讨论】:

    • 应该是 var obj = new JSONObject(list) 吗?
    • 我不熟悉 GOT 库,所以我不确定。 @RowanC​​pan>
    • @Will 将其移入异步函数后,我现在没有得到控制台的响应。即使尝试 RowanC 的建议,它也没有给我任何东西。
    • 什么是redd?退回后console.log(list)会得到什么?
    • @Will 在 GOT 中,您从一个 const 变量开始,在我的例子中,它被命名为 redd,它提供了前缀 url 、响应类型和标头。此外,为了console.log(list),我必须摆脱我的 JSON 捕获,然后它会打印有关调用的所有信息,而不是来自 API 的 JSON 响应。
    【解决方案3】:

    你已经定义了一个异步函数,但是你还没有等待它的结果。

    我认为在呼叫站点前添加 await 可以解决此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      • 2014-11-09
      相关资源
      最近更新 更多