如何从这些JSON对象中过滤出值

时间:2019-06-24 15:57:38

标签: javascript node.js json

我正在使用一个在线API,该API返回给我JSON代码,但我不知道如何过滤JSON代码中的“名称”值。我尝试过滤的JSON存在于许多对象中,并且尝试了许多不同的方法。如何从每个对象中获取“名称”的值?我尝试做.people.name,但是我总是得到一个空白输出,或者它只会说“ [object Object] [object Object] [object Object] [object Object] [object Object]”

JSON响应:

{
  "message": "success",
  "number": 6,
  "people": [
    {
      "craft": "ISS",
      "name": "Oleg Kononenko"
    },
    {
      "craft": "ISS",
      "name": "David Saint-Jacques"
    },
    {
      "craft": "ISS",
      "name": "Anne McClain"
    },
    {
      "craft": "ISS",
      "name": "Alexey Ovchinin"
    },
    {
      "craft": "ISS",
      "name": "Nick Hague"
    },
    {
      "craft": "ISS",
      "name": "Christina Koch"
    }
  ]
}

我在NodeJS中的代码:

request('http://api.open-notify.org/astros.json', (error, response, html) => {

        if (!error && response.statusCode == 200)
        {
            let astroJSON = JSON.parse(html);

            let astroNum = astroJSON.number;
            let astroNames = JSON.stringify(astroJSON.people); // This is what I need help with!
            console.log("Number: " + astroNum);
            console.log("Crew names: " + astroNames); // Return the JSON response that I sent above. 
        }
    });

2 个答案:

答案 0 :(得分:2)

尝试使用Array#prototype#map

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/painel/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /painel/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ painel/index.php [L] 
</IfModule>


<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

将代码更改为此:

const data = `{
  "message": "success",
  "number": 6,
  "people": [
    {
      "craft": "ISS",
      "name": "Oleg Kononenko"
    },
    {
      "craft": "ISS",
      "name": "David Saint-Jacques"
    },
    {
      "craft": "ISS",
      "name": "Anne McClain"
    },
    {
      "craft": "ISS",
      "name": "Alexey Ovchinin"
    },
    {
      "craft": "ISS",
      "name": "Nick Hague"
    },
    {
      "craft": "ISS",
      "name": "Christina Koch"
    }
  ]
}`;

const obj = JSON.parse(data);

const astroNames = obj.number;

// Join the crew names with a comma
const crew = obj.people.map(x => x.name).join(', ');
console.log(crew);

答案 1 :(得分:-1)

您必须在阵列上进行映射。参见https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

例如

astroJSON.people.map(person => person.name)

将记录人员姓名的数组:

["Oleg Kononenko", "David Saint-Jacques", "Anne McClain", "Alexey Ovchinin", "Nick Hague", "Christina Koch"]

要删除括号和逗号,可以使用

astroNames.join(' ')

返回字符串"Oleg Kononenko David Saint-Jacques Anne McClain Alexey Ovchinin Nick Hague Christina Koch"

请参见Array.prototype.join