react-native - 解析 json 在本机平面列表中做出反应
问题描述
我正在尝试在 Flatlist 中呈现数据,但它没有任何解决方案?
我的代码:
import React from 'react'
import { ActivityIndicator, FlatList, Text, View } from 'react-native'
import styles from '../components/style.js'
export default class Nachrichten extends React.Component {
constructor(props) {
super(props);
this.state = {
nachrichtenapidata: [],
loading: true
};
}
async componentDidMount() {
try {
const nachrichtenApiCall = await fetch(URL);
const nachrichten = await nachrichtenApiCall.json();
this.setState({nachrichtenapidata: nachrichten.data, loading: false});
console.log(this.state.nachrichtenapidata);
} catch(err) {
console.log("Error fetching data-----------", err);
}
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator />
</View>
);
}
return (
<View style={{ flex: 1, paddingTop: 20 }}>
<FlatList
data={this.state.nachrichtenapidata}
renderItem={({ item }) => (
<Text>
-> {item}
</Text>
)}
keyExtractor={(index) => index}
/>
</View>
);
}
}
我的输出是:
任何解决方案-我认为我在做一些愚蠢的事情,但看不到它....?
Tnx 全部求助!
解决方案
您可以使用 JSON.parse() :
async componentDidMount() {
try {
const nachrichtenApiCall = await fetch(URL);
const nachrichten = await nachrichtenApiCall.json();
const json = JSON.parse(nachrichten);
this.setState({
nachrichtenapidata: json.data,
loading: false
});
console.log(this.state.nachrichtenapidata);
} catch(err) {
console.log("Error fetching data-----------", err);
}
}
您确定服务器正在返回有效的 json 响应吗?
推荐阅读
- java - 如何通过批处理文件运行java的多个类文件
- r - 如何在 r 中按 chron 日期配对和计算行
- mysql - 从结果选择更改列
- vue.js - Vue3:警告将带有过渡的路线更改为包含过渡的路线
- scala - Scala3中基于通配符的存在类型
- swift - 为什么 SpriteKit 引用不显示 SVG 文件?
- java - 如何在方法中编辑数组?
- flutter - 用 TextSpan 替换文本后光标位置错误
- mongodb - 使用 .NET Core 2.1 在 LINUX RHEL7 上的 MongoDB GSSAPI 身份验证失败
- pine-script - 返回最新价格并检查先前价格与其相交的位置