简体   繁体   English

如何在 React ZD3E28535C74CCEE29182AEB3CF837E 中从 WEB API 中指定 JSON 数据

[英]How to specify JSON data from WEB API in React Axios

Just a simple question here, I'm using ASP.NET Web API and ReactJS, and the instrument for reading JSON I'm currently using Axios, this is my JSON data: Just a simple question here, I'm using ASP.NET Web API and ReactJS, and the instrument for reading JSON I'm currently using Axios, this is my JSON data:

[
    {
        "id": 1,
        "name": "Count Duck",
        "age": 3
    },
    {
        "id": 4,
        "name": "Count Dracula",
        "age": 288
    },
    {
        "id": 5,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 6,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 7,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 8,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 9,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 10,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 11,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 12,
        "name": "Frankenstein",
        "age": 45
    },
    {
        "id": 13,
        "name": "Frankenstein",
        "age": 45
    }
]

This is my axios request:这是我的 axios 请求:

async getDataAxios(){
    const response = await axios.get("https://localhost:5001/monsters");
    const data = await response.data;
    this.setState({ monsters: data, loading: false });
    console.log(data);
  } catch(err) {
      console.log(err)
 }
}

currently I'm using this, but I get an error: (Uncaught TypeError: Cannot read property 'name' of undefined)目前我正在使用它,但出现错误:(未捕获的类型错误:无法读取未定义的属性“名称”)

static renderMonstersTable(monsters) {
    return (
      <table className='table table-striped' aria-labelledby="tabelLabel">
        <thead>
          <tr>
            <th>Username</th>
            <th>Image</th>
          </tr>
        </thead>
        <tbody>
          {monsters.map(({items}) =>
            <tr key="">
              <td>{items.name}</td>
              <td>{items.age}</td>
            </tr>
          )}
        </tbody>
      </table>
    );
  }

Any help or correction would be appreciated!任何帮助或更正将不胜感激!

So your the data your working with (monsters / on the component actually this.state.monsters because it looks like you're using react) is an array of objects.因此,您使用的数据(实际上是组件上的怪物/ this.state.monsters,因为看起来您正在使用反应)是一个对象数组。 When you do monsters.map you are looping over each element in your monsters array.当您执行 monsters.map 时,您将遍历 Monsters 数组中的每个元素。 The function you pass to monsters.map will take a parameter (your first '???') which will point to each object as it loops over the array.你传递给怪物的 function。map 将采用一个参数(你的第一个'???'),它将指向每个 object,因为它在数组上循环。 Inside that function you can use that parameter to access keys on the monster object.在 function 内部,您可以使用该参数访问怪物 object 上的密钥。 Which is how we fill out the rest of the '???'我们如何填写“???”的 rest

It would look like this:它看起来像这样:

(
  monsters.map(monster => (
    <tr key={monster.id}>
      <td>{monster.name}</td>
      <td>{monster.age}</td>
    <tr/>
  )
)
async getDataAxios(){
  try{
    const response = await axios.get("https://localhost:5001/monsters");
    const data = response.data; /// you don't really need await here
    this.setState({ monsters: data, loading: false });
  } catch(err) {
      console.log(err)
 }
}

loop through like this.像这样循环。 Do not use curly braces {} on monster parameter will fix ur issue.不要在怪物参数上使用花括号 {} 将解决您的问题。

monsters.map( monster =>
            `<tr key=${monster.id}>
              <td>${monster.name}</td>
              <td>${monster.age}</td>`

It's possible to do what you're trying to do.有可能做你想做的事。 If you still want to de-structure your array items, you need to change {items} to what you're using.如果您仍想解构您的数组项,则需要将{items}更改为您正在使用的内容。 In this case {id, name, age} .在这种情况下{id, name, age}

The difference between both ways would be:两种方式的区别在于:

{monsters.map(({ id, name, age}) => (
  <tr key={id}>
  <td>{name}</td>
  <td>{age}</td>
  </tr>
))}

And

{monsters.map(monster => (
  <tr key={monster.id}>
  <td>{monster.name}</td>
  <td>{monster.age}</td>
  </tr>
))}

Here is a working example, using a placeholder API: Codesandbox这是一个使用占位符 API 的工作示例: Codesandbox

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM