【问题标题】:Problems getting data from a local json file in React在 React 中从本地 json 文件获取数据的问题
【发布时间】:2023-01-07 02:39:02
【问题描述】:

我的 src 文件夹中有一个包含本地 JSON 文件的 React 项目。我正在使用 fetch 并在获取文件时遇到问题。

 [
  {
    "id": 1,
    "field1": "some data",
    "field2": 63,
    "field3": ["info", "info", "info"],
    "array1":[14, 35, 22, 22, 7],
    "array2": [8, 21,  14, 13, 5],
    "Link": "https://trailhead.salesforce.com"
  }
]

async function loadJSON (url) {
 const res = await fetch(url);
 return await JSON.parse(JSON.stringify(res));
}

loadJSON('../file.json')
 .then(data => {
 console.log(data);
});

This is what I get in the console.

When my return statement is return await (res) I get this

任何帮助将不胜感激。

【问题讨论】:

    标签: reactjs json fetch


    【解决方案1】:

    如果它是本地的,您可以直接将其导入到您的应用中,例如:

    import data from './data.json'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-09
      • 2020-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 2014-09-19
      • 2021-11-10
      相关资源
      最近更新 更多