【发布时间】:2021-02-08 22:49:08
【问题描述】:
我是 React Native 的新手。我想用 fetch 从 json 中提取一个值来做一个简单的测试。但我不明白,如何从 Json 中选择特定的键。总是,我有未定义的回报。我试图用this post 修改我的代码,但它不起作用。我之前尝试过解析,但他不想要,因为它已经是一个对象了。
这是我的代码:
checkLogin = () => {
const { name } = this.state;
const { surname } = this.state;
fetch('https://ffn.extranat.fr/webffn/_recherche.php?go=ind&idrch=' + name + '%20' + surname, {
method: 'GET',
}).then((response) => response.json())
.then((responseJson) => {
if (responseJson.ind == 'Individu non trouv\u00e9 !') {
alert("Id incorrect")
}
else {
alert("Id correct");
}
alert(JSON.stringify(responseJson.ind))
}).catch((error) => {
console.error(error);
});
}
这是我的 JSON 格式:
[{"iuf":"1366701","ind":"LEBRUN L\u00e9o (2000) H FRA - CN BEAUPREAU","sex":"#1e90ff","clb":"CN BEAUPREAU"}]
我知道我的请求有效,因为当我运行此代码alert(JSON.stringify(responseJson))时,它会返回整个 json。所以我不知道,如何解决未定义的返回。
问候
【问题讨论】:
标签: json react-native fetch