2017-02-26 123 views
0
{"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); 

我在做什麼錯?你可以幫我嗎?

+0

[獲取的JSONObject的可能的複製從JSONArray](http://stackoverflow.com/questions/7634518/getting-jsonobject-from-jsonarray) –

回答

0

您確定要查找此JSONArray索引0處的物品嗎?要獲得JSONArrayJSONObject,請致電JSONArray array = object.getJSONArray("key"),然後JSONObject newobject = array.getJSONObject(index)

+0

我想從「forecastday」的第一個項目是不是0呢? –

+0

@ZockerBros你可以上傳完整的json,通過json驗證器進行驗證嗎? – Feelfree

+0

我是這樣做的:[鏈接](https://www.youtube.com/watch?v=UuGIAomlU1I&t=1h29m12s) –

0
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; 
} 

}