简体   繁体   English

如何在反应中获取 json 数据?

[英]How to fetch json data in react?

useEffect(async () =>
{
    const fetchData = async () =>
    {
        const result = await axios("")
        setposts(result.data)
    }
    fetchData();
    
    
}, [])

Rendering:渲染:

posts.map(post => <li>{post.name}</li>)

Json File: Json 文件:

[
   {
       "id": 1,
       "vendor": "Gap",
       "name": "Men's Pullover Sweatshirt",
       "image_src": ["https://cdn.shopify.com/s/files/1/0455/2176/4502/files/Sweater_0.jpg?v=1603266982"],
       "price": "74",
       "tag": "T-shirt",
       "compare_at_price": "200",
       "options": [
           {
               "id": "1010",
               "name": "Size",
               "value": "US 8"
           }, {
               "id": "1011",
               "name": "Size",
               "value": "US 9"
           }, {
               "id": "1012",
               "name": "Size",
               "value": "US 10"
           }, {
               "id": "1013",
               "name": "Size",
               "value": "US 11"
           }, {
               "id": "1014",
               "name": "Size",
               "value": "US 13"
           }
       ]
   },

im facing issue to retrieve the data from json file Like im not able render the data so please help over this我面临从 json 文件中检索数据的问题 就像我无法呈现数据一样,请帮助解决这个问题

Getting error while mapping the data映射数据时出错

im facing issue to retrieve the data from json file Like im not able render the data so please help over this我面临从 json 文件中检索数据的问题 就像我无法呈现数据一样,请帮助解决这个问题

useEffect(async () =>
{
    const fetchData = async () =>
    {
        const result = await axios("https://cdn.shopify.com/s/files/1/0455/2176/4502/files/products.json")
        setposts(result.data)
    }
    fetchData();
    
    
}, [])

Rendering:渲染:

posts.map(post => <li>{post.name}</li>)

Json File: Json 文件:

[
   {
       "id": 1,
       "vendor": "Gap",
       "name": "Men's Pullover Sweatshirt",
       "image_src": ["https://cdn.shopify.com/s/files/1/0455/2176/4502/files/Sweater_0.jpg?v=1603266982"],
       "price": "74",
       "tag": "T-shirt",
       "compare_at_price": "200",
       "options": [
           {
               "id": "1010",
               "name": "Size",
               "value": "US 8"
           }, {
               "id": "1011",
               "name": "Size",
               "value": "US 9"
           }, {
               "id": "1012",
               "name": "Size",
               "value": "US 10"
           }, {
               "id": "1013",
               "name": "Size",
               "value": "US 11"
           }, {
               "id": "1014",
               "name": "Size",
               "value": "US 13"
           }
       ]
   },

im facing issue to retrieve the data from json file Like im not able render the data so please help over this我面临从 json 文件中检索数据的问题 就像我无法呈现数据一样,请帮助解决这个问题

Getting error while mapping the data映射数据时出错

im facing issue to retrieve the data from json file Like im not able render the data so please help over this我面临从 json 文件中检索数据的问题 就像我无法呈现数据一样,请帮助解决这个问题

useEffect(() =>
{
    const fetchData = async () =>
    {
        const result = await axios.get("https://cdn.shopify.com/s/files/1/0455/2176/4502/files/products.json")
        setposts(result.data)
    }
    fetchData();
    
    
}, [])


posts?.map(post => <li>{post.name}</li>)

The issue lies with this file: https://cdn.shopify.com/s/files/1/0455/2176/4502/files/products.json问题在于这个文件: https://cdn.shopify.com/s/files/1/0455/2176/4502/files/products.Z466DEEC76ECDF324FCA6D3857DF

It returns a an invalid JSON string.它返回一个无效的 JSON 字符串。 So when your axios gets its contents, it sets your posts variable to a simple string, which obviously cannot be mapped through.因此,当您的 axios 获取其内容时,它将您的posts变量设置为一个简单的字符串,显然无法映射。 Hence the error.因此错误。

The reason axios does not convert it into a JS array is because the file does not contain valid JSON to parse. axios 没有将其转换为 JS 数组的原因是因为该文件不包含要解析的有效 JSON。

The reason the JSON in the file is invalid is because it has a trailing comma after the very last object of the array.文件中的 JSON 无效的原因是它在数组的最后一个 object 后面有一个逗号。 See image:见图片: 在此处输入图像描述

The reason this isn't working for you is, result.data is a string, not an array.这对你不起作用的原因是, result.data是一个字符串,而不是一个数组。

You'll have to convert result.data to array with JSON.parse您必须使用JSON.parse将 result.data 转换为数组

Also, the json you're using has a training comma which has to be handled for JSON.parse to work此外,您正在使用的 json 有一个训练逗号,必须处理它才能使JSON.parse工作

following code worked for me:以下代码对我有用:

export default function App() {
  const [posts, setposts] = useState([]);
  useEffect(async () => {
    const fetchData = async () => {
      const result = await axios.get(
        "https://cdn.shopify.com/s/files/1/0455/2176/4502/files/products.json"
      );
      let regex = /\,(?!\s*?[\{\[\"\'\w])/g;
      let correct = result.data.replace(regex, "");
      setposts(JSON.parse(correct));
    };
    fetchData();
  }, []);

  return (
    <div className="App">
      {posts.map((post) => (
        <li>{post.name}</li>
      ))}
    </div>
  );
}

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

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