Axios详细返回嵌套对象

Vin*_*yen 1 javascript api rest axios

我有一个axios获取请求,如:

axios.get(url + '?hapikey=' + copyFrom)
    .then(res => console.log(res.data));
Run Code Online (Sandbox Code Playgroud)

返回类似于以下内容的响应:

{ name: 'Name',
  anObject: [
      {
          id: 1,
          nestedObject: [Object]
      }
  ]
}
Run Code Online (Sandbox Code Playgroud)

如何获得的响应nestedObject以显示其字段/值对,或者是直接在中查询它的唯一方法.then

J. *_*rdo 6

深入打印对象的最简单方法是使用JSON.stringify,例如:

axios.get(url + '?hapikey=' + copyFrom)
  .then(res => console.log(JSON.stringify(res.data)));
Run Code Online (Sandbox Code Playgroud)

正如@PatrickRoberts所说,您可以使用可选参数进行漂亮打印

axios.get(url + '?hapikey=' + copyFrom)
  .then(res => console.log(JSON.stringify(res.data, null, 2)));
Run Code Online (Sandbox Code Playgroud)

  • 也可以使用可选参数进行漂亮的打印,例如`JSON.stringify(res.data,null,2)`缩进具有2个空格的嵌套结构。 (2认同)