如何在react native上从本地JSON文件中获取数据?

llew8vvj  于 4个月前  发布在  React
关注(0)|答案(8)|浏览(57)

如何存储本地文件(如JSON),然后从控制器中获取数据?

pgx2nnw8

pgx2nnw81#

从React Native 0.4.3开始,你可以像这样读取本地JSON文件:

const customData = require('./customData.json');

字符串
然后像普通JS对象一样访问customData。

sqougxex

sqougxex2#

ES6/ES 2015版本:

import customData from './customData.json';

字符串

yquaqz18

yquaqz183#

对于ES6/ES 2015,您可以直接导入,如:

// example.json
{
    "name": "testing"
}

// ES6/ES2015
// app.js
import * as data from './example.json';
const word = data.name;
console.log(word); // output 'testing'

字符串
如果你使用typescript,你可以像这样声明json模块:

// tying.d.ts
declare module "*.json" {
    const value: any;
    export default value;
}

dgjrabp2

dgjrabp24#

使用此

import data from './customData.json';

字符串

niwlg2el

niwlg2el5#

以下是获取本地JSON文件的方法-

ES6版本:

import customData from './customData.json';import customData from './customData';
如果它在**.js文件而不是.json**中,则导入如下-

import { customData } from './customData';

字符串
有关更多说明/理解,请参考示例-Live working demo

kcugc4gi

kcugc4gi6#

也许你可以使用AsyncStoragesetItemgetItem.并将数据存储为字符串,然后使用the json parser再次将其转换为json.

5jvtdoz2

5jvtdoz27#

看看这个Github问题:
https://github.com/facebook/react-native/issues/231
他们正在尝试require非JSON文件,特别是JSON。现在没有方法做到这一点,所以你要么像@CocoOS提到的那样使用AsyncStorage,要么你可以写一个小的本地模块来做你需要做的事情。

fcwjkofz

fcwjkofz8#

我有相同的要求,如以上我发现上述方法很有帮助,只是为了增加更多的清晰度,我已经按照下面的方法从.js文件或.json文件访问json数组如下

第一种方法:
**Step - 1:**创建一个js文件,并将自定义数据添加到json array中,如下所示
data.js

export const DATA = [
    {
      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'
    }
  ];
  
  export default DATA;

字符串

**第2步:**现在访问并显示在一个列表中从App.js文件,所以首先我已经像下面这样导入并显示在列表中

import DATA from './data';

App.js

import React from 'react';
import DATA from './data';
import { SafeAreaView,StyleSheet, View, Text}  from 'react-native';

const App = () => {

  return (
    <SafeAreaView style={{flex:1}}>
    <View style={{ flex: 1, backgroundColor: 'white' }}>
        {DATA.map((item)=> 
        {return(
          <View style={{flex:1,borderBottomColor:'red',borderBottomWidth:2,marginHorizontal:12,justifyContent:'center'}} key={item.id}>
           <Text numberOfLines={1} style={{fontSize: 24, fontWeight: '400'}}>{item.title}</Text>
        </View>
        )})}
    </View>
    </SafeAreaView>
)
}


然后下面的截图是两种方法的结果。只需添加App.jsdata.jsitems.json就存在于同一个文件夹中。

第二种方法:
**Step - 1:**创建一个json文件,并将自定义数据添加到json array
items.json

[
    {
      "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"
    }
  ]

**第2步:**现在访问并显示在一个列表中从App.js文件,所以首先我已经像下面这样导入并显示在列表中

import DATA from './items.json';

App.js

import React from 'react';
import DATA from './items.json';
import { SafeAreaView,StyleSheet, View, Text}  from 'react-native';

const App = () => {

  return (
    <SafeAreaView style={{flex:1}}>
    <View style={{ flex: 1, backgroundColor: 'white' }}>
        {DATA.map((item)=> 
        {return(
          <View style={{flex:1,borderBottomColor:'red',borderBottomWidth:2,marginHorizontal:12,justifyContent:'center'}} key={item.id}>
           <Text numberOfLines={1} style={{fontSize: 24, fontWeight: '400'}}>{item.title}</Text>
        </View>
        )})}
    </View>
    </SafeAreaView>
)
}

export default App;


x1c 0d1x的数据

相关问题