从JSONObject中获取JSONArray中的JSONObject数据

时间:2017-02-26 15:57:50

标签: arrays json android-studio

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

我做错了什么?你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

您确定,您希望此0的索引为JSONArray的项目吗?要从JSONArray获取JSONObject,请致电JSONArray array = object.getJSONArray("key"),然后JSONObject newobject = array.getJSONObject(index)

答案 1 :(得分: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;
}

}