我必须读取一个json文件,我遵循以下代码:stackoverflow
尝试注释中显示的示例,但它对我不起作用。
我必须阅读本地存储的文件,但没有成功。
你能帮我吗?
链接:Codesandbox
代码:
fetch("/user.json")
.then(res => res.json())
.then(res => res.results)
.then(res => {
console.log(res);
let u = res.map((suggestion, i) => ({
label: suggestion.email,
name: suggestion.name.first,
surname: suggestion.name.last,
address: suggestion.location.street.name,
email: suggestion.email,
picture: suggestion.picture.thumbnail
}));
let options =
inputLength === 0
? []
: u.filter(suggestion => {
const keep =
count < 5 &&
suggestion.label &&
suggestion.label.slice(0, inputLength).toLowerCase() ===
inputValue;
if (keep) count += 1;
return keep;
});
this.setState({ options });
})
.catch(e => console.log("Er:", e));
答案 0 :(得分:0)
您可以只使用import或要求如下:
const data = require("./myFile.json")
import data from "./myFile.json"