2013-06-04 33 views
-2

我使用www.openweathermap.org預測。 thisi forecat結果:http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139嗨。如何解析下面的JSON「openweather ** strong text **」:

JSONObject coordObj = getObject("coord", jObj); 
    Latitude=getFloat("lat", coordObj); 
    Longitude=getFloat("lon", coordObj); 
    JSONObject coordObj = getObject("city", jObj); 
    id=getFloat("id", coordObj); 
    name=getFString("name", coordObj); 
    JSONObject sysObj = getObject("sys", jObj); 
    Country=getString("country", sysObj); 
    Sunrise=getInt("sunrise", sysObj)); 
    Sunset=getInt("sunset", sysObj)); 
JSONObject jlist = jObj.getObject("list"); 

JSONObject JSONWeather = jArr.getJSONObject(0); 
    Condition_id==getInt("id", JSONWeather); 
    condition_description=getString("description", JSONWeather); 
    condition=getString("main", JSONWeather); 
    condition_icongetString("icon", JSONWeather); 

    JSONObject mainObj = getObject("main", jObj); 
    Humidity=getInt("humidity", mainObj); 
    Pressure=getInt("pressure", mainObj); 
    MaxTemp=getFloat("temp_max", mainObj); 
    MinTemp(getFloat("temp_min", mainObj); 
    Temp=getFloat("temp", mainObj); 

    // Wind 
    JSONObject wObj = getObject("wind", jObj; 
    Speed=getFloat("speed", wObj); 
     Deg=getFloat("deg", wObj); 

    // Clouds 
    JSONObject cObj = getObject("clouds", jObj); 
    Perc=getInt("all", cObj); 

請如何循環天氣陣列?

回答

0

首先,名單是不是一個JSONObject它是一個數組,所以你應該得到它這樣做的:

JSONArray jlist = (JSONArray) jObj.get("list"); 

根據正在使用的語法可以更改庫的,但道理是一樣的,我使用json簡單的lib解釋。

之後,你應該遍歷列表排列,這樣的事情:

for (int i = 0; i < jlist.size(); i++){ 
// get all your objects and your weather array 
// to get your weather array the logic is the same: 

JSONArray jArrayWeather = (JSONArray) jObj.get("weather"); 

    for (int j = 0; j < jArrayWeather ; j++){ 
     //and here you can get your id, main, description and icon using j index 
     JSONObject currentObj = (JSONObject) jArrayWeather.get(j); 
     String main = (String) currentObj.get("main"); 
    } 
} 

我沒有測試此代碼,所以按照這個想法,並嘗試自己做。看看here,因爲我們可以看到你沒有使用json的經驗

+0

非常感謝您的回答先生Victor Oliveira :) – Mochini

+0

如果這對您有幫助,請將其標記爲正確答案:D,這很愉快。 –

+0

我還沒有測試,但這是非常有益的謝謝你:D – Mochini

0

這是另一個例子。 (儘管不同的JSON格式)。

try{ 
     JSONArray list=json.getJSONArray("list"); 
     for(int indx=0;indx<MAX_FORCAST_FRAGMENT;indx++) { 
      JSONArray weather = list.getJSONObject(indx).getJSONArray("weather"); 
      String weatherIconString=setWeatherIcon(weather.getJSONObject(0).getInt("id"), 
        0,100); 
      forcastData[indx][0]=weatherIconString; 

      JSONObject main=list.getJSONObject(indx).getJSONObject("main"); 
      JSONObject wind=list.getJSONObject(indx).getJSONObject("wind"); 

      String detailsFieldString= weather.getJSONObject(0).getString("description").toUpperCase(Locale.US); 
      String humidityFieldString="Humidity: " + main.getString("humidity") + "%"; 
      String windFieldString= "Wind: " + wind.getString("speed") + " Km/H"; 
      // populate the list view// 
      int forecastFragmentId=getResources().getIdentifier("forcast_layout_" + (indx+1), "id", getPackageName()); 

      tv=(TextView)findViewById(forecastFragmentId).findViewById(R.id.details_field); 
      tv.setText(detailsFieldString); 
      saveData(F_DETAILS+indx,detailsFieldString); 

      tv=(TextView)findViewById(forecastFragmentId).findViewById(R.id.weather_icon); 
      tv.setText(weatherIconString); 
      saveData(F_ICON+indx,weatherIconString); 

      tv=(TextView)findViewById(forecastFragmentId).findViewById(R.id.wind_field); 
      tv.setText(windFieldString); 
      saveData(F_WIND+indx,windFieldString); 

      tv=(TextView)findViewById(forecastFragmentId).findViewById(R.id.humidity_field); 
      tv.setText(humidityFieldString); 
      saveData(F_HUMIDITY+indx,humidityFieldString); 

      c = Calendar.getInstance(); 
      int currentDate=c.get(Calendar.DAY_OF_MONTH); 
      saveTime(LAST_FORCAST_TIME,currentDate); 
     } 


    }catch(Exception e){ 
     Log.e("SimpleWeather", "One or more fields not found in the JSON data in renderForecastData"); 
    } 
}