如何使用 React-Native-fs 读取 JSON 文件
How to read JSON file with React-Native-fs
我有一个包含内容的 asset.json 文件,需要在 react-native 应用程序中读取它。
我已经认为必须手动将它复制到本机实现,并且我可以验证该文件是否存在(并且可读:-rw-r--r--)。
因为它在那里并且我正在使用承诺来获得它,请告诉我输出是否仍然是:
{"_40":0,"_65":0,"_55":null,"_72":null}
而不是文件的内容。
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
const asset_content = await RNFS.readFile(path);
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
输出为:
[10:03:55] I | ReactNativeJS ▶︎ BLAH EXISTS
[10:03:55] I | ReactNativeJS ▶︎ 'local asset_content:', '{"_40":0,"_65":0,"_55":null,"_72":null}'
[10:03:55] I | ReactNativeJS ▶︎ 'local assets:', { _40: 0, _65: 0, _55: null, _72: null }
另一方面,如果我出于某种原因使用 promisify
,如某些帖子中所建议的那样,应用程序会在调用已读取的承诺文件时冻结。
这里的代码应用了更改:
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
const readFileAsync = promisify(RNFS.readFile);
const asset_content = await readFileAsync(path);
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
及其输出:
[10:24:16] I | ReactNativeJS ▶︎ BLAH EXISTS
文件不大,只有81行有效JSON。
现在,如果我使用 promise 来检查是否有这样的异常:
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
const readFileAsync = promisify(RNFS.readFile);
let asset_content = null;
readFileAsync(path, 'utf8')
.then((str) => {
console.log("got result: ", str);
asset_content = str;
})
.catch((e) => {
console.log("got error:", e);
});
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
我仍然没有异常,结果为空:
[10:45:00] I | ReactNativeJS ▶︎ BLAH EXISTS
[10:45:00] I | ReactNativeJS ▶︎ 'local asset_content:', null
[10:45:00] I | ReactNativeJS ▶︎ 'local assets:', null
当我摆脱 promisify 并离开 promise 处理时,我回到了我开始的地方:
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
let asset_content = null;
RNFS.readFile(path, 'utf8')
.then((str) => {
console.log("got result: ", str);
asset_content = str;
})
.catch((e) => {
console.log("got error:", e);
});
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
输出:
[10:49:45] I | ReactNativeJS ▶︎ BLAH EXISTS
[10:49:45] I | ReactNativeJS ▶︎ 'local asset_content:', null
[10:49:45] I | ReactNativeJS ▶︎ 'local assets:', null
[10:49:45] I | ReactNativeJS ▶︎ 'got result: ', '{"_40":0,"_65":0,"_55":null,"_72":null}'
请帮忙。文件处理对我们的应用程序至关重要。
更新:
这是文件 asset.json 的内容,它被 ASSET_FILE:
引用
{
"protobuf": [
{
"name": "tiny-fovapp-4c",
"lite": false,
"compressed": false,
"selected": false
},
{
"name": "tiny-yolo-4c-quantized",
"lite": true,
"compressed": true,
"selected": false
},
{
"name": "tiny-yolo-4c",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilePath": "ai/protobuf/tiny-yolo-4c.pb",
"labelsFilePath": "ai/protobuf/tiny-yolo-4c-labels.txt",
"lite": false,
"compressed": false,
"selected": true
},
{
"name": "tiny-yolo-4c",
"lite": true,
"compressed": false,
"selected": false
}
],
"testImages": [
{
"name": "IMG_6924.jpg",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/IMG_6924.jpg"
},
{
"name": "IMG_6924.png",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/IMG_6924.png"
},
{
"name": "IMG_6929.jpg",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/IMG_6929.jpg"
},
{
"name": "Part1.png",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/Part1.png"
},
{
"name": "Part1_10.png",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/Part1_10.png"
}
],
"parts": [
{
"name": "Part1",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilename": "ai/models/Part1.png",
"drawingFilename": "ai/drawings/Part1.png",
"annotationFilename": "ai/annotations/Part1.xml"
},
{
"name": "Part2",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilename": "ai/models/Part2.png",
"drawingFilename": "ai/drawings/Part2.png",
"annotationFilename": "ai/annotations/Part2.xml"
},
{
"name": "Part3",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilename": "ai/models/Part3.png",
"drawingFilename": "ai/drawings/Part3.png",
"annotationFilename": "ai/annotations/Part3.xml"
}
]
}
Steps to read json file in react-native applications :-
1.将其导入您的组件
import ASSET_FILE from '../assets/files/asset.json';
// import filename from 'path to your asset file';
2。现在在函数中
jsonParser(){
var data = JSON.parse(JSON.stringify(ASSET_FILE));
var tempProtobuf = []
var temptestImages= []
var tempparts= []
tempProtobuf = data.Protobuf
temptestImages = data.testImages
tempparts = data.parts
}
// Now three of your objects are there in tempProtobuf, temptestImages, tempparts array respectively.
Iterate if or save in state if you want to show in your application..
希望这对您有所帮助...谢谢 :)
我终于设法将静态文件的内容复制到应用程序文档目录中,并通过使用 NRFechtBlob.fs 实现和提供的编码参数来读取和维护它,如下所示:
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);
呸。没想到2019年文件处理会这么痛苦
我有一个包含内容的 asset.json 文件,需要在 react-native 应用程序中读取它。 我已经认为必须手动将它复制到本机实现,并且我可以验证该文件是否存在(并且可读:-rw-r--r--)。 因为它在那里并且我正在使用承诺来获得它,请告诉我输出是否仍然是:
{"_40":0,"_65":0,"_55":null,"_72":null}
而不是文件的内容。
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
const asset_content = await RNFS.readFile(path);
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
输出为:
[10:03:55] I | ReactNativeJS ▶︎ BLAH EXISTS
[10:03:55] I | ReactNativeJS ▶︎ 'local asset_content:', '{"_40":0,"_65":0,"_55":null,"_72":null}'
[10:03:55] I | ReactNativeJS ▶︎ 'local assets:', { _40: 0, _65: 0, _55: null, _72: null }
另一方面,如果我出于某种原因使用 promisify
,如某些帖子中所建议的那样,应用程序会在调用已读取的承诺文件时冻结。
这里的代码应用了更改:
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
const readFileAsync = promisify(RNFS.readFile);
const asset_content = await readFileAsync(path);
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
及其输出:
[10:24:16] I | ReactNativeJS ▶︎ BLAH EXISTS
文件不大,只有81行有效JSON。 现在,如果我使用 promise 来检查是否有这样的异常:
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
const readFileAsync = promisify(RNFS.readFile);
let asset_content = null;
readFileAsync(path, 'utf8')
.then((str) => {
console.log("got result: ", str);
asset_content = str;
})
.catch((e) => {
console.log("got error:", e);
});
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
我仍然没有异常,结果为空:
[10:45:00] I | ReactNativeJS ▶︎ BLAH EXISTS
[10:45:00] I | ReactNativeJS ▶︎ 'local asset_content:', null
[10:45:00] I | ReactNativeJS ▶︎ 'local assets:', null
当我摆脱 promisify 并离开 promise 处理时,我回到了我开始的地方:
const path = RNFetchBlob.fs.dirs.DocumentDir + '/' + ASSET_FILENAME;
if (await RNFS.exists(path)){
console.log("BLAH EXISTS");
} else {
console.log("BLAH DOES NOT EXIST");
}
let asset_content = null;
RNFS.readFile(path, 'utf8')
.then((str) => {
console.log("got result: ", str);
asset_content = str;
})
.catch((e) => {
console.log("got error:", e);
});
console.log("local asset_content:", asset_content);
const assets = JSON.parse(asset_content);
console.log("local assets:", assets);
输出:
[10:49:45] I | ReactNativeJS ▶︎ BLAH EXISTS
[10:49:45] I | ReactNativeJS ▶︎ 'local asset_content:', null
[10:49:45] I | ReactNativeJS ▶︎ 'local assets:', null
[10:49:45] I | ReactNativeJS ▶︎ 'got result: ', '{"_40":0,"_65":0,"_55":null,"_72":null}'
请帮忙。文件处理对我们的应用程序至关重要。
更新: 这是文件 asset.json 的内容,它被 ASSET_FILE:
引用{
"protobuf": [
{
"name": "tiny-fovapp-4c",
"lite": false,
"compressed": false,
"selected": false
},
{
"name": "tiny-yolo-4c-quantized",
"lite": true,
"compressed": true,
"selected": false
},
{
"name": "tiny-yolo-4c",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilePath": "ai/protobuf/tiny-yolo-4c.pb",
"labelsFilePath": "ai/protobuf/tiny-yolo-4c-labels.txt",
"lite": false,
"compressed": false,
"selected": true
},
{
"name": "tiny-yolo-4c",
"lite": true,
"compressed": false,
"selected": false
}
],
"testImages": [
{
"name": "IMG_6924.jpg",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/IMG_6924.jpg"
},
{
"name": "IMG_6924.png",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/IMG_6924.png"
},
{
"name": "IMG_6929.jpg",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/IMG_6929.jpg"
},
{
"name": "Part1.png",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/Part1.png"
},
{
"name": "Part1_10.png",
"parentFolder": "/data/user/0/com.foviar/files/",
"filePath": "ai/testimgs/Part1_10.png"
}
],
"parts": [
{
"name": "Part1",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilename": "ai/models/Part1.png",
"drawingFilename": "ai/drawings/Part1.png",
"annotationFilename": "ai/annotations/Part1.xml"
},
{
"name": "Part2",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilename": "ai/models/Part2.png",
"drawingFilename": "ai/drawings/Part2.png",
"annotationFilename": "ai/annotations/Part2.xml"
},
{
"name": "Part3",
"parentFolder": "/data/user/0/com.foviar/files/",
"modelFilename": "ai/models/Part3.png",
"drawingFilename": "ai/drawings/Part3.png",
"annotationFilename": "ai/annotations/Part3.xml"
}
]
}
Steps to read json file in react-native applications :-
1.将其导入您的组件
import ASSET_FILE from '../assets/files/asset.json';
// import filename from 'path to your asset file';
2。现在在函数中
jsonParser(){
var data = JSON.parse(JSON.stringify(ASSET_FILE));
var tempProtobuf = []
var temptestImages= []
var tempparts= []
tempProtobuf = data.Protobuf
temptestImages = data.testImages
tempparts = data.parts
}
// Now three of your objects are there in tempProtobuf, temptestImages, tempparts array respectively.
Iterate if or save in state if you want to show in your application..
希望这对您有所帮助...谢谢 :)
我终于设法将静态文件的内容复制到应用程序文档目录中,并通过使用 NRFechtBlob.fs 实现和提供的编码参数来读取和维护它,如下所示:
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);
呸。没想到2019年文件处理会这么痛苦