使用reactjs获取本地json数据
我在第一次工作中遇到json错误。这对我和我作为候选人的工作都非常重要。我怎样才能解决这个问题?非常感谢 相关类别:使用reactjs获取本地json数据,json,reactjs,react-router,react-redux,data-mapping,Json,Reactjs,React Router,React Redux,Data Mapping,我在第一次工作中遇到json错误。这对我和我作为候选人的工作都非常重要。我怎样才能解决这个问题?非常感谢 相关类别: constructor() { super(); this.state = { data:[], } } componentDidMount(){ fetch('../JsonFiles/routes.json'). then(response => response.json()). then(findrespons
constructor() {
super();
this.state = {
data:[],
}
}
componentDidMount(){
fetch('../JsonFiles/routes.json').
then(response => response.json()).
then(findresponse => this.setState ({ data: findresponse.data }));
}
render() {
return (
<div>
fasfasfa
{
this.state.data.map((dynamicData, key) =>
<div>
<span> {dynamicData.routes.origin.cityName} </span>
<span> {dynamicData.routes.destinations[{}].cityName} </span>
</div>
)
}
</div>
);
}
我得到了这个错误:
一,
未处理的拒绝语法错误:JSON中位置0处的意外标记<
匿名函数
D:/development/js projects/materialtutorial/src/Components/TrialForm/FlightSearch.js:13
10 |
11 |组件安装{
12 |获取“../JsonFiles/routes.json”。
13 | thenresponse=>response.json。
14 | thenfindresponse=>this.setState{data:findresponse.data};
15 |
16|
您可以导入JSON文件
import * as jsonFile from '../JsonFiles/routes.json';
在构造函数的状态中定义它:
constructor() {
super();
this.state = {
data: jsonFile
}
}
es2015版本:
import jsonFile from '../JsonFiles/routes.json';
现在您在jsonFile中有了所有可用的json数据,您可以在任何地方使用它。您可以尝试使用绝对路径吗?如下:/home/test/user/JsonFiles/routes.json请在说明中指定您的json文件路径。谢谢。在您发表评论之前,我像您的解决方案一样解决了它。不过,感谢您的贡献。
import jsonFile from '../JsonFiles/routes.json';