`json()`函数返回一个包含数组的对象,我想要一个对象的数组。

huangapple go评论57阅读模式
英文:

Function json() returns an Object with an array inside, I want an array of objects

问题

我正在使用Fetch从JSON服务器获取数据,并将其保存在connection常量中。在获取数据后,我使用connection.json()将JSON转换为JavaScript对象。但我无法对这个对象进行映射,因为它是一个内部包含数组的对象。我想将它转换成一个对象数组。我应该怎么做?

  useEffect(() => {
    list();
  }, []);

  async function list(){
    try{
      const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
      const list = await connection.json();
      setItems(list);
      console.log(items); // 返回一个内部包含数组的对象
    }catch(error){
      console.log(error);
    }
  }
英文:

I'm using Fetch to get data from a json server and save it in the connection constant. After getting the data, I use connection.json() to transform the json into a javascript object. But I can't map this object, because it is an Object with an array inside. I would like to turn it into an array of objects. How should I proceed?

  useEffect(() => {
    list();
  }, [ ]);

  async function list(){
    try{
      const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
      const list = await connection.json();
      setItems(list);
      console.log(items); // Returns an Object with an array inside
    }catch(error){
      console.log(error);
    }
  }

答案1

得分: 0

响应包含节点"lista"。如果直接访问它,您将获得所需的结果。

async function list(){
  try{
    const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
    const list = await connection.json();
    setItems(list.lista); // 直接访问'lista'属性
    console.log(items);
  }catch(error){
    console.log(error);
  }
}
英文:

the response contains the node lista. If you access it directly, you will get the desired result.

async function list(){
  try{
    const connection = await fetch("https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db");
    const list = await connection.json();
    setItems(list.lista); // Access the 'lista' property directly
    console.log(items);
  }catch(error){
    console.log(error);
  }
}

答案2

得分: -1

if you want to render the array of objects,
you can use something like this,

  const [items, setItems] = useState([]);
  async function list() {
    try {
      const connection = await fetch(
        'https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db'
      );
      const list = await connection.json();
      setItems(list.lista);
      console.log(list.lista); // used this to get access of the array
    } catch (error) {
      console.log(error);
    }
  }

return (
      {items.map((i) => (
        <span>
          <div>{i.id}</div>
          <div>{i.nome}</div>
        </span>
      ))}
)
英文:

if you want to render the array of objects,
you can use something like this,

  const [items, setItems] = useState([]);
  async function list() {
    try {
      const connection = await fetch(
        &#39;https://my-json-server.typicode.com/priscilasilvaalves/priscilasilvaalves.github.io/db&#39;
      );
      const list = await connection.json();
      setItems(list.lista);
      console.log(list.lista); // used this to get access of the array
    } catch (error) {
      console.log(error);
    }
  }

return (
      {item.map((i) =&gt; (
        &lt;span&gt;
          &lt;div&gt;{i.id}&lt;/div&gt;
          &lt;div&gt;{i.nome}&lt;/div&gt;
        &lt;/span&gt;
      ))}
)

huangapple
  • 本文由 发表于 2023年7月24日 00:12:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76749217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定