【发布时间】:2017-01-24 00:35:58
【问题描述】:
我正在从 PHP 文件中检索 JSON 字符串,如下所示:
async fetchAvailableSets(){
try {
let response = await fetch('http://example.com/getdata.php', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
let responseJson = await response.json()
.then((responseData) => {
console.log(responseData) /* This displays the JSON text */
RNFS.writeFile(RNFS.DocumentDirectoryPath + 'info.json', responseData)
.then(() => {
RNFS.readFile(RNFS.DocumentDirectoryPath + 'info.json')
.then((contents) => {
console.log(contents) /* Nothing is displayed */
})
})
})
}
.
.
.
}
如果我将 writeFile 行更改为:
RNFS.writeFile(RNFS.DocumentDirectoryPath + 'info.json', responseData.toString())
那么我写入文件的所有内容都是“[object Object]”。看起来这是一个字符串,而不是一个合法的对象。
我觉得它应该不需要将它转换为字符串,因为它已经是一个字符串。此外,为什么将其转换为字符串使其成为对象有点令人困惑。
我做错了什么?
【问题讨论】:
-
如果你做
JSON.stringify(responseData),它会给你同样的东西吗? -
@MattAft 谢谢,成功了!
标签: json react-native