gpt4 book ai didi

android - react-native 本地 json 文件

转载 作者:行者123 更新时间:2023-11-29 16:39:20 25 4
gpt4 key购买 nike

您好,我想从本地 Machine 的 JSON 文件中解析 Json 数据。这是我的代码在 React native 中从本地文件获取 Json 解析。这是我的另一行代码控制台 console.log(data_local);

最佳答案

请尝试添加以下代码:
“StudentList.json”文件中的 Json 代码将如下所示:

[{"id": 1,"name": "Student1"}, 
{"id": 2, "name": "Student2"},
{"id": 3, "name": "Student3"},
{"id": 4, "name": "Student4"},
{"id": 5, "name": "Student5"},
{"id": 6, "name": "Student6"},
{"id": 7, "name": "Student7"},
{"id": 8, "name": "Student8"},
{"id": 9, "name": "Student9"},
{"id": 10, "name": "Student10"}]

访问json数据的代码如下:

import React, { Component } from 'react';
import { View, Text, FlatList} from 'react-native';
import studentList from '../data/StudentList.json';

class DemoApp extends Component {
render() {
return (
<View style={{flex: 1, flexDirection: 'column'}}>
<Text >
Student List
</Text>
<FlatList
data={studentList}
showsVerticalScrollIndicator={false}
renderItem={({item}) =>
<View >
<Text>{item.name}</Text>
</View>
}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
}
}

export default DemoApp;

关于android - react-native 本地 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51633120/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com