【问题标题】:Getting data from JSONObject in JSONArray from JSONObject从 JSONObject 获取 JSONArray 中的 JSONObject 数据
【发布时间】:2017-02-26 15:57:49
【问题描述】:
{"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);

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

【问题讨论】:

标签: arrays json android-studio


【解决方案1】:

您确定要在此JSONArray 的索引0 处的项目吗?要从JSONObject 获取JSONArray,请调用JSONArray array = object.getJSONArray("key"),然后调用JSONObject newobject = array.getJSONObject(index)

【讨论】:

  • 我想要 "forecastday" 中的第一项不是 0 吗?
  • @ZockerBros 你能上传完整的 json,通过 json 验证器验证吗?
  • 我的做法和他一样:link
【解决方案2】:
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;
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 1970-01-01
    相关资源
    最近更新 更多