下面是我用来读取已经下载并存储在"TemporaryDirectoryPath“中的JSON文件的代码。
var fileName = getFileNameFromUrl(url);
RNFS.downloadFile({
fromUrl: url,
toFile: `${RNFS.TemporaryDirectoryPath}/`+fileName
}).promise.then(r => {
var content = RNFS.readFile(`${RNFS.TemporaryDirectoryPath}/`+fileName, 'utf8');
console.log(content);
});我得到了一个"Promise“类型的对象,如下所示
Promise {_40: 0, _65: 0, _55: null, _72: null}
_40: 0
_55: "{"show_explanation":1,"answer_result":2, //More json content will be here}"
_65: 1
_72: null如何从Promise对象中读取内容?
发布于 2017-12-15 18:07:15
为什么不直接用" import“来导入呢?
import AnyName from './pathToYourJSONfile';我必须在这里写,添加评论的低代表!:/
发布于 2018-12-25 18:15:51
您已经知道了RNFS.readFile() return Promise。
所以你只需要学习如何使用Promise。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
var fileName = getFileNameFromUrl(url);
RNFS.downloadFile({
fromUrl: url,
toFile: `${RNFS.TemporaryDirectoryPath}/` + fileName
})
.then(r => {
RNFS.readFile(`${RNFS.TemporaryDirectoryPath}/` + fileName, 'utf8')
.then(content => {
console.log(content);
})
});发布于 2019-11-27 18:36:20
我也遇到了同样的问题,最终通过以下代码片段获得了内容:
let asset_content = null;
try {
await RNFetchBlob.fs.readFile(assetFile_path, 'utf8')
.then((data) => {
asset_content = data;
console.log("got data: ", data);
})
.catch((e) => {
console.error("got error: ", e);
})
} catch (err) {
console.log('ERROR:', err);
}
const assets = JSON.parse(asset_content);您可能还必须确保内容已另存为“utf8”
https://stackoverflow.com/questions/47829697
复制相似问题