react-native - JSON 解析错误:无法识别的令牌'<' - 在 React Native 中
问题描述
我在 React Native 中遇到问题。消息是这样的JSON Parse error: Unrecognized token '<'
。当我获取时会发生这种情况。
import React from 'react';
import { FlatList, ActivityIndicator, Text, View } from 'react-native';
export default class FetchExample extends React.Component {
constructor(props) {
super(props);
this.state ={ isLoading: true}
}
componentDidMount() {
return fetch("http://10.0.3.2:80/api/user", {
headers: {
"Content-Type": "application/json",
"Accept": "application/json, text/plain, */*"
},
method: 'GET'
}).then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
dataSource: responseJson.user,
}, function(){
});
})
.catch((error) => {
console.error(error);
});
}
render() {
if(this.state.isLoading) {
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.email}</Text>}
keyExtractor={({id}, index) => id}
/>
</View>
);
}
}
这是我的 JSON。我在 Laravel 中构建。
return->response()->json(['user', $user])
["user",[{"id":1,"name":"","email":"danang@gmail.com","email_verified_at":null,"created_at":null,"updated_at":null}]]
解决方案
我认为你的 json 格式不正确请试试这个 json
{
"user":[{
"id":1,
"name":"",
"email":"danang@gmail.com",
"email_verified_at":null,
"created_at":null,
"updated_at":null}]}
推荐阅读
- java - Mqtt 客户端:订阅后获取保留消息
- r - 提取连接矩阵单元的边缘
- javascript - 为什么函数体内的 for of 循环没有给我传统 for 循环所做的预期输出?
- javascript - 如何根据入口点(例如使用的按钮)显示架构数据
- c# - 如何在 C# 中拆分混合字符串
- docker - 运行多 pod 的 nginx 入口 504 超时
- javascript - 为 fullcalendar 4.2.0 上的事件添加动画背景
- c# - public PictureBox[] pictureb => Controls.OfType 是什么意思
().ToArray(); - git - 如何使用 github api 获取 github 存储库中的所有目录和文件?
- java - 整数数组中重复值的数量