Arrays 如果要呈现子对象集合,请改用数组。(我想访问temp的值)
这些是天气API返回的值。我想获取值Arrays 如果要呈现子对象集合,请改用数组。(我想访问temp的值),arrays,api,react-native-android,Arrays,Api,React Native Android,这些是天气API返回的值。我想获取值temp {coord: {…}, weather: Array(1), base: "stations", main: {…}, visibility: 10000, …} base: "stations" clouds: {all: 100} cod: 200 coord: {lon: 73.86, lat: 18.52} dt: 1598779072 id: 125
temp
{coord: {…}, weather: Array(1), base: "stations", main: {…}, visibility: 10000, …}
base: "stations"
clouds: {all: 100}
cod: 200
coord: {lon: 73.86, lat: 18.52}
dt: 1598779072
id: 1259229
main: {temp: 26.46, feels_like: 25.59, temp_min: 26.46, temp_max: 26.46, pressure: 1005, …}
name: "Pune"
rain: {1h: 0.16}
sys: {country: "IN", sunrise: 1598748587, sunset: 1598793658}
timezone: 19800
visibility: 10000
weather: [{…}]
wind: {speed: 7.06, deg: 245}
__proto__: Object
这是我的React原生文件,App.js
import React, { useState, useEffect } from 'react';
import { StyleSheet, Text, View , TextInput , Button, TouchableOpacity} from 'react-native';
const api ={
key :"api key",
base:"https://api.openweathermap.org/data/2.5/"
}
const App = () => {
const [query, setQuery] = useState('');
const [weather, setWeather] = useState({});
const search =(evt)=>{
if (evt.key === "Enter") {
fetch(`${api.base}weather?q=${query}&units=metric&APPID=${api.key}`)
.then(res=>res.json())
.then(result => {
setWeather(result);
setQuery('');
console.log(result);
});
}
}
return (
<View><Text>Enter Country name to get Temp</Text>
<TextInput title="press me" onChange={e => setQuery(e.target.value)} value={query} onKeyPress={search} placeholder="Search..."/>
<Text>{weather.name}</Text>
<Text>{weather.main.temp}</Text>
</View>
)
};
export default App;
import React,{useState,useffect}来自“React”;
从“react native”导入{样式表、文本、视图、文本输入、按钮、TouchableOpacity};
常量api={
密钥:“api密钥”,
基数:“https://api.openweathermap.org/data/2.5/"
}
常量应用=()=>{
const[query,setQuery]=useState(“”);
const[weather,setWeather]=useState({});
常量搜索=(evt)=>{
如果(evt.key==“输入”){
fetch(`api.base}weather?q=${query}&units=metric&APPID=${api.key}`)
.then(res=>res.json())
。然后(结果=>{
天气(结果);
setQuery(“”);
控制台日志(结果);
});
}
}
返回(
输入国家名称以获取临时数据
setQuery(e.target.value)}value={query}onKeyPress={search}占位符=“search…”/>
{weather.name}
{weather.main.temp}
)
};
导出默认应用程序;
有人能帮帮我吗