【发布时间】:2021-05-28 02:41:07
【问题描述】:
我是反应原生的新手,我想创建一个简单的应用程序来获取 JSON 数据。
这是我的 json 文件。
[
{
"fruit": "Apple",
"size": "Large",
"color": "Red"
},
{
"fruit": "Orange",
"size": "big",
"color": "Orange"
}
]
这是我的反应原生代码
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = { data: '' };
}
componentDidMount = () => {
fetch('https://othersite.my.json', {
method: 'GET'
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
this.setState({
data: responseJson
})
})
.catch((error) => {
console.error(error);
});
}
render() {
return (
<View style={styles.container}>
<Text>
{this.state.data}
//for debug {this.state.data.fruit}
</Text>
</View>
);
}
}
但它不起作用。
【问题讨论】:
-
您是否设置了正确的 my.json 路径。它可能类似于 ./my.json 或 ./../my.json
-
抱歉我的描述不清楚,这不是我主机中自己的json文件,它使用了另一个站点的HTTPS URL路径,谢谢
-
控制台里有什么?
标签: javascript arrays json react-native