【问题标题】:Hi. How do I parse the following JSON "openweather**strong text**":你好。如何解析以下 JSON“openweather**strong text**”:
【发布时间】:2013-05-31 04:56:51
【问题描述】:

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

请问如何循环天气数组?

【问题讨论】:

    标签: android weather


    【解决方案1】:

    这是另一个例子。 (不过对于不同的 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");
        }
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 如果对您有帮助,请将其标记为正确答案 :D,很高兴。
      • 我还没有测试过,但这很有帮助,谢谢:D
      猜你喜欢
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多