从JSONObject的在JSONArray从JSONObject的
{"location":{
"name":"New York","region":"New York","country":"United States of America","lat":40.71,"lon":-74.01,"tz_id":"America/New_York","localtime_epoch":1488124171,"localtime":"2017-02-26 10:49"},
"forecast":{
"forecastday":[{"date":"2017-02-26","date_epoch":1488067200,"day":{"maxtemp_c":5.2,"mintemp_c":1.0,"avgtemp_c":3.5,"maxwind_kph":28.1,
"astro":{"sunrise":"06:34 AM",...
获取数据,我编程的天气应用,但是当我想访问,例如,日期,那么Android Studio中说有:用于预测没有价值。这是我如何获得数据:从JSONObject的在JSONArray从JSONObject的
JSONObject jsonObject = new JSONObject(data);
[...]
JSONObject forecastObject = Utils.getObject("forecast", jsonObject);
JSONArray forecastArray = forecastObject.getJSONArray("forecastday");
JSONObject forecastWeather = forecastArray.getJSONObject(0);
weather.currentCondition.setDate(Utils.getString("date", forecastWeather));
JSONObject dayObject = Utils.getObject("day", forecastWeather);
我在做什么错?你可以帮我吗?
您确定要查找此JSONArray
索引0
处的物品吗?要获得JSONArray
从JSONObject
,请致电JSONArray array = object.getJSONArray("key")
,然后JSONObject newobject = array.getJSONObject(index)
。
我想从“forecastday”的第一个项目是不是0呢? –
@ZockerBros你可以上传完整的json,通过json验证器进行验证吗? – Feelfree
我是这样做的:[链接](https://www.youtube.com/watch?v=UuGIAomlU1I&t=1h29m12s) –
public static Weather getWeather(String data){
Weather weather = new Weather();
//creat JSONObject from data
try {
JSONObject jsonObject = new JSONObject(data);
Place place = new Place();
JSONObject locationObject = Utils.getObject("location", jsonObject);
place.setLat(Utils.getFloat("lat", locationObject));
place.setLon(Utils.getFloat("lon", locationObject));
place.setCity(Utils.getString("name", locationObject));
place.setCountry(Utils.getString("country", locationObject));
place.setRegion(Utils.getString("region", locationObject));
place.setLocaltime(Utils.getString("localtime", locationObject));
weather.place=place;
JSONObject currentObject = Utils.getObject("current", jsonObject);
weather.currentCondition.setLastupdated(Utils.getString("last_updated", currentObject));
weather.currentCondition.setTemperature(Utils.getString("temp_c", currentObject));
weather.currentCondition.setWindkph(Utils.getString("wind_kph", currentObject));
weather.currentCondition.setHumidity(Utils.getFloat("humidity", currentObject));
weather.currentCondition.setCloud(Utils.getInt("cloud", currentObject));
weather.currentCondition.setFeelslike(Utils.getDouble("feelslike_c", currentObject));
weather.wind.setWinddir(Utils.getString("wind_dir", currentObject));
weather.currentCondition.setPreciption(Utils.getString("precip_mm", currentObject));
weather.currentCondition.setPressure(Utils.getFloat("pressure_mb", currentObject));
JSONObject iconObject = Utils.getObject("condition", currentObject);
weather.currentCondition.setIcon(Utils.getString("icon", iconObject));
weather.currentCondition.setDescription(Utils.getString("text", iconObject));
return weather;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
这是我如何从网站apixu.com得到我的数据的功能
这是我如何连接:
public class WeatherHttpClient {
public String getWeatherData(String place){
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
connection = (HttpURLConnection) (new URL(Utils.BASE_URL + place)).openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.setDoInput(true);
connection.connect();
//Read response
StringBuffer stringBuffer = new StringBuffer();
inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while((line = bufferedReader.readLine())!=null){
stringBuffer.append(line+ "\r\n");
}
inputStream.close();
connection.disconnect();
return stringBuffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
[获取的JSONObject的可能的复制从JSONArray](http://*.com/questions/7634518/getting-jsonobject-from-jsonarray) –