我一直在试图找出答案,但不知道我在做什么错。我也是Aurelia,Typescript和Axios的新手。

后端为我提供了一个要解析为Javascript对象的对象的JSON数组。凉。对于我的虚假数据,我正在使用JSONplaceholder。当我解析时,返回的是[object Object](请参阅底部的图像链接)。我做错了什么?最终,我想提取特定数据,例如info.name,并显示名称。

测试

import axios from 'axios';
const apiURL = 'https://jsonplaceholder.typicode.com/users';

declare var $: any;


export class Test {
    info: string;
    infoX: string;
    public constructor () {
      axios.get(apiURL)
        .then(response => {
          this.info = JSON.stringify(response.data)
          this.infoX = JSON.parse(this.info);
          console.log(this.info);
          console.log(this.infoX);
        })
        .catch(error => console.log(error));
    }
}


test.html

<template>
  <pre style="margin-top: 200px">${info}</pre>
  <pre style="margin-top: 200px">${infoX}</pre>
</template>


screenshot of what the console log and view displays

最佳答案

以下链接有助于消除我的困惑:simple explanation of JSON.parse and JSON.stringify

然后在迭代数组的注释中听Jame的建议,并从服务器返回数据。

测试

import axios from 'axios';
const apiURL = 'https://jsonplaceholder.typicode.com/users';

export class Data {
    infos: string;
    public constructor () {
      axios.get(apiURL)
        .then(response => {
          this.infos = response.data;
          console.log(this.infos);
        })
        .catch(error => console.log(error));
    }
}


test.html

<template>
    <ul>
        <li repeat.for="info of infos">
          Name: ${info.name}
        </li>
    </ul>
</template>

10-05 20:49
查看更多