Question

i use www.openweathermap.org FORECAST. thisi the result of forecat: http://api.openweathermap.org/data/2.5/forecast?lat=35&lon=139

    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);

please how to loop the weather array ?

Was it helpful?

Solution

First, list is not a JsonObject it's an array, so you should get it doing:

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

Depending of which library you are using the syntax can change but the logic is the same, I'm explaining using json simple lib.

after that you should iterate your list array, something like this:

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");
    }
}

I didn't test this code, so follow the idea and try to do it yourself. Take a look here as we can see you haven't experience with json

OTHER TIPS

Here is another example. (For a different JSON format though).

 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");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top