[
{
"id": 1,
"title": "The Hunger Games"
},
{
"id": 2,
"title": "Harry Potter and the Order of the Phoenix"
},
{
"id": 3,
"title": "To Kill a Mockingbird"
},
{
"id": 4,
"title": "Pride and Prejudice"
},
{
"id": 5,
"title": "Twilight"
},
{
"id": 6,
"title": "The Book Thief"
},
{
"id": 7,
"title": "The Chronicles of Narnia"
},
{
"id": 8,
"title": "Animal Farm"
},
{
"id": 9,
"title": "Gone with the Wind"
},
{
"id": 10,
"title": "The Shadow of the Wind"
},
{
"id": 11,
"title": "The Fault in Our Stars"
},
{
"id": 12,
"title": "The Hitchhiker's Guide to the Galaxy"
},
{
"id": 13,
"title": "The Giving Tree"
},
{
"id": 14,
"title": "Wuthering Heights"
},
{
"id": 15,
"title": "The Da Vinci Code"
}
]
8条答案
按热度按时间pgx2nnw81#
从React Native 0.4.3开始,你可以像这样读取本地JSON文件:
字符串
然后像普通JS对象一样访问customData。
sqougxex2#
ES6/ES 2015版本:
字符串
yquaqz183#
对于ES6/ES 2015,您可以直接导入,如:
字符串
如果你使用typescript,你可以像这样声明json模块:
型
dgjrabp24#
使用此
字符串
niwlg2el5#
以下是获取本地JSON文件的方法-
ES6版本:
import customData from './customData.json';
或import customData from './customData';
如果它在**
.js
文件而不是.json
**中,则导入如下-字符串
有关更多说明/理解,请参考示例-Live working demo
kcugc4gi6#
也许你可以使用AsyncStoragesetItem和getItem.并将数据存储为字符串,然后使用the json parser再次将其转换为json.
5jvtdoz27#
看看这个Github问题:
https://github.com/facebook/react-native/issues/231
他们正在尝试
require
非JSON文件,特别是JSON。现在没有方法做到这一点,所以你要么像@CocoOS提到的那样使用AsyncStorage,要么你可以写一个小的本地模块来做你需要做的事情。fcwjkofz8#
我有相同的要求,如以上我发现上述方法很有帮助,只是为了增加更多的清晰度,我已经按照下面的方法从
.js
文件或.json
文件访问json
数组如下第一种方法:
**Step - 1:**创建一个
js
文件,并将自定义数据添加到json array
中,如下所示data.js
字符串
**第2步:**现在访问并显示在一个列表中从
App.js
文件,所以首先我已经像下面这样导入并显示在列表中型
App.js
型
然后下面的截图是两种方法的结果。只需添加
App.js
和data.js
,items.json
就存在于同一个文件夹中。第二种方法:
**Step - 1:**创建一个
json
文件,并将自定义数据添加到json array
中items.json
型
**第2步:**现在访问并显示在一个列表中从
App.js
文件,所以首先我已经像下面这样导入并显示在列表中型
App.js
型
x1c 0d1x的数据