【发布时间】:2018-11-10 19:04:27
【问题描述】:
我的 React Native 应用程序获取 API 数据,我需要打印响应的第一个索引,但它不是,并获取所有“臭氧”,例如在父数组的所有子元素中,当我打印 val[0] 时映射我没有打印任何东西
我的代码|
export default class App extends Component {
constructor(props) {
super(props);
this.state = { isLoading: true, dataSource: null };
}
async componentDidMount() {
let API_WEATHER =
"https://api.weatherbit.io/v2.0/forecast/daily?city=Raleigh,NC&key={API_KEY}";
fetch(API_WEATHER)
.then(response => response.json())
.then(responseJson => {
console.log(responseJson.data);
this.setState({
isLoading: false,
dataSource: responseJson.data
});
})
.catch(error => {
console.log(error);
});
}
render() {
if (this.state.isLoading) {
return (
<View style={{ flex: 1, padding: 20 }}>
<ActivityIndicator size="large" />
</View>
);
}
let weather= this.state.dataSource.map((val, key) => {
return (
<Text key={key}>
{val.ozone}
</Text>
);
});
return (
<ScrollView style={styles.container}>
<ScrollView>
<View>
<Text>{weather}</Text>
</View>
</ScrollView>
</ScrollView>
);
}
在我记录响应 JSON obj 时的这部分代码
.then(responseJson => {
console.log(responseJson.data);
console.log(responseJson.data[0]);
console.log(responseJson.data[0].datetime);
}
我有我需要的东西,但是当在 View 中打印它们时,我有 Erroe 看看图片
【问题讨论】:
-
`console.log(responseJson.data);`的输出是什么?
-
@Akrion我有一个对象数组
-
@Akrion 检查更新问题
标签: javascript reactjs api react-native fetch