-2

我使用 www.openweathermap.org FORECAST。这是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);

请问如何循环天气数组?

4

2 回答 2

0

首先, list 不是 JsonObject 它是一个数组,所以你应该这样做:

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

根据您使用的库,语法可能会发生变化,但逻辑是相同的,我将使用 json simple 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");
    }
}

我没有测试这段代码,所以按照这个想法尝试自己做。看看这里,我们可以看到你没有使用过 json 的经验

于 2013-06-04T17:51:08.660 回答
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");
    }
}
于 2016-06-07T16:35:54.340 回答