首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试处理从NodeJS中的API调用返回的json文件-错误:未定义-为什么?

尝试处理从NodeJS中的API调用返回的json文件-错误:未定义-为什么?
EN

Stack Overflow用户
提问于 2022-07-01 17:33:01
回答 2查看 30关注 0票数 1

我正在从一个APIcall接收一个JSON文件(参见下面)。现在,我想访问JSON文件的不同键,分别表示不同的值。但是,我在控制台中得到一条“未定义”消息。有什么想法吗?

这是调用API并处理JSON文件的代码:

代码语言:javascript
复制
   const input = req.body.var_1;
    console.log('Request Query:' + JSON.stringify({ "user_input": input}));
    const articles = [];
    const sent = 'test';
    const api_url = '...';
    const options = {
        method: 'POST',
        body: JSON.stringify({"user_input": input}),
        headers: {'Content-Type': 'application/json'}
    }

    const response = await fetch(api_url, options);
    const results = await Promise.all([response.json()]);
    console.log(results);
    data = JSON.parse(JSON.stringify(results))
    console.log(data);
    console.log(data.Topic);

这是我得到的控制台输出:

代码语言:javascript
复制
Request Query:{"user_input":"xgboost"}
[
  {
    Topic: {
      '0': 'random forest , Machine Learning and Wine Quality: Finding a good wine using multiple classifications',
      '1': 'decision tree learning , Gradient Boost for Classification',
      '2': 'feature selection , Understanding Multilabel Text Classification and the related process',
      '3': 'decision tree learning , Gradient Boost for Regression',
      '4': '<strong class="markup--strong markup--h3-strong">Understanding AdaBoost</strong> , Anyone starting to learn Boosting technique should start first with AdaBoost or…'
    },
    'URL/PDF': {
      '0': 'https://dev.to//leading-edje/machine-learning-and-wine-quality-finding-a-good-wine-using-multiple-classifications-4kho',
      '1': 'https://dev.to//xsabzal/gradient-boost-for-classification-2f15',
      '2': 'https://dev.to//botreetechnologies/understanding-multilabel-text-classification-and-the-related-process-n66',
      '3': 'https://dev.to//xsabzal/gradient-boost-for-regression-1e42',
      '4': 'https://towardsdatascience.com/understanding-adaboost-2f94f22d5bfe'
    },
    Doc_Type: {
      '0': 'article',
      '1': 'article',
      '2': 'article',
      '3': 'article',
      '4': 'article'
    }
  }
]
[
  {
    Topic: {
      '0': 'random forest , Machine Learning and Wine Quality: Finding a good wine using multiple classifications',
      '1': 'decision tree learning , Gradient Boost for Classification',
      '2': 'feature selection , Understanding Multilabel Text Classification and the related process',
      '3': 'decision tree learning , Gradient Boost for Regression',
      '4': '<strong class="markup--strong markup--h3-strong">Understanding AdaBoost</strong> , Anyone starting to learn Boosting technique should start first with AdaBoost or…'
    },
    'URL/PDF': {
      '0': 'https://dev.to//leading-edje/machine-learning-and-wine-quality-finding-a-good-wine-using-multiple-classifications-4kho',
      '1': 'https://dev.to//xsabzal/gradient-boost-for-classification-2f15',
      '2': 'https://dev.to//botreetechnologies/understanding-multilabel-text-classification-and-the-related-process-n66',
      '3': 'https://dev.to//xsabzal/gradient-boost-for-regression-1e42',
      '4': 'https://towardsdatascience.com/understanding-adaboost-2f94f22d5bfe'
    },
    Doc_Type: {
      '0': 'article',
      '1': 'article',
      '2': 'article',
      '3': 'article',
      '4': 'article'
    }
  }
]
undefined

很高兴得到任何帮助,泰斯

EN

回答 2

Stack Overflow用户

发布于 2022-07-01 17:38:44

注意输出中的[ ] -- results (或data )是一个数组,因此Topic将是results[0].Topic (数组本身没有属性Topic,只有它的元素有一个,因此是undefined结果)。

但是:--它根本不应该是一个数组,它只是一个数组,因为这里有一个数组:

代码语言:javascript
复制
const results = await Promise.all([response.json()]);

Promise.all将等待一组承诺,并返回其解析值的数组。您提供一个具有一个承诺的数组,因此您将得到一个具有一个值的数组,而不是只获取值本身。

但是,只使用一个承诺就可以使用Promise.all没有意义,因此您可以去掉该部分,从而也可以去掉数组:

代码语言:javascript
复制
const results = await response.json()
console.log(results.Topic)

(正如你所看到的,整个JSON.parse(JSON.stringify(...))恶作剧也是多余的。)

票数 1
EN

Stack Overflow用户

发布于 2022-07-01 17:39:24

看起来,对象嵌套在数组中,因此必须使用data[0].Topic访问数组中的第一个项。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72832840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档