尝试解析JSON的OpenWeather API

时间:2018-08-14 00:50:23

标签: javascript android json openweathermap

我正在尝试为openweathermap API解析当前api。我对如何获取当前数据和预测信息感到非常困惑。这是我得到的json:

 "cod":"200",
"message":0.0074,
  "cnt":39,
"list":[
  {
     "dt":1534215600,
     "main":{
        "temp":293.24,
        "temp_min":292.346,
        "temp_max":293.24,
        "pressure":1021.77,
        "sea_level":1028.21,
        "grnd_level":1021.77,
        "humidity":100,
        "temp_kf":0.89
     },
     "weather":[
        {
           "id":500,
           "main":"Rain",
           "description":"light rain",
           "icon":"10n"
        }
     ],
     "clouds":{
        "all":20
     },
     "wind":{
        "speed":2.51,
        "deg":275.001
     },
     "rain":{
        "3h":0.0050000000000008
     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-14 03:00:00"
  },
  {
     "dt":1534226400,
     "main":{
        "temp":292.3,
        "temp_min":291.706,
        "temp_max":292.3,
        "pressure":1020.99,
        "sea_level":1027.42,
        "grnd_level":1020.99,
        "humidity":100,
        "temp_kf":0.6
     },
     "weather":[
        {
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
        }
     ],
     "clouds":{
        "all":0
     },
     "wind":{
        "speed":2.52,
        "deg":294.505
     },
     "rain":{

     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-14 06:00:00"
  },
  {
     "dt":1534237200,
     "main":{
        "temp":291.07,
        "temp_min":290.77,
        "temp_max":291.07,
        "pressure":1020.65,
        "sea_level":1027.03,
        "grnd_level":1020.65,
        "humidity":100,
        "temp_kf":0.3
     },
     "weather":[
        {
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
        }
     ],
     "clouds":{
        "all":0
     },
     "wind":{
        "speed":1.31,
        "deg":225.5
     },
     "rain":{

     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-14 09:00:00"
  },
  {
     "dt":1534248000,
     "main":{
        "temp":293.286,
        "temp_min":293.286,
        "temp_max":293.286,
        "pressure":1020.78,
        "sea_level":1027.17,
        "grnd_level":1020.78,
        "humidity":100,
        "temp_kf":0
     },
     "weather":[
        {
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"02d"
        }
     ],
     "clouds":{
        "all":8
     },
     "wind":{
        "speed":2.83,
        "deg":234.501
     },
     "rain":{

     },
     "sys":{
        "pod":"d"
     },
     "dt_txt":"2018-08-14 12:00:00"
  },
  {
     "dt":1534258800,
     "main":{
        "temp":298.671,
        "temp_min":298.671,
        "temp_max":298.671,
        "pressure":1020.76,
        "sea_level":1027.15,
        "grnd_level":1020.76,
        "humidity":92,
        "temp_kf":0
     },
     "weather":[
        {
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01d"
        }
     ],
     "clouds":{
        "all":0
     },
     "wind":{
        "speed":2.71,
        "deg":259.5
     },
     "rain":{

     },
     "sys":{
        "pod":"d"
     },
     "dt_txt":"2018-08-14 15:00:00"
  },
  {
     "dt":1534269600,
     "main":{
        "temp":300.7,
        "temp_min":300.7,
        "temp_max":300.7,
        "pressure":1019.76,
        "sea_level":1026.18,
        "grnd_level":1019.76,
        "humidity":83,
        "temp_kf":0
     },
     "weather":[
        {
           "id":500,
           "main":"Rain",
           "description":"light rain",
           "icon":"10d"
        }
     ],
     "clouds":{
        "all":24
     },
     "wind":{
        "speed":3.66,
        "deg":285.503
     },
     "rain":{
        "3h":1.11
     },
     "sys":{
        "pod":"d"
     },
     "dt_txt":"2018-08-14 18:00:00"
  },
  {
     "dt":1534280400,
     "main":{
        "temp":298.464,
        "temp_min":298.464,
        "temp_max":298.464,
        "pressure":1019.68,
        "sea_level":1025.97,
        "grnd_level":1019.68,
        "humidity":83,
        "temp_kf":0
     },
     "weather":[
        {
           "id":500,
           "main":"Rain",
           "description":"light rain",
           "icon":"10d"
        }
     ],
     "clouds":{
        "all":48
     },
     "wind":{
        "speed":3.27,
        "deg":289.504
     },
     "rain":{
        "3h":1.61
     },
     "sys":{
        "pod":"d"
     },
     "dt_txt":"2018-08-14 21:00:00"
  },
  {
     "dt":1534291200,
     "main":{
        "temp":297.882,
        "temp_min":297.882,
        "temp_max":297.882,
        "pressure":1020,
        "sea_level":1026.37,
        "grnd_level":1020,
        "humidity":82,
        "temp_kf":0
     },
     "weather":[
        {
           "id":500,
           "main":"Rain",
           "description":"light rain",
           "icon":"10n"
        }
     ],
     "clouds":{
        "all":36
     },
     "wind":{
        "speed":2.37,
        "deg":275.004
     },
     "rain":{
        "3h":0.13
     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-15 00:00:00"
  },
  {
     "dt":1534302000,
     "main":{
        "temp":295.242,
        "temp_min":295.242,
        "temp_max":295.242,
        "pressure":1021.11,
        "sea_level":1027.53,
        "grnd_level":1021.11,
        "humidity":94,
        "temp_kf":0
     },
     "weather":[
        {
           "id":802,
           "main":"Clouds",
           "description":"scattered clouds",
           "icon":"03n"
        }
     ],
     "clouds":{
        "all":32
     },
     "wind":{
        "speed":1.26,
        "deg":313.002
     },
     "rain":{

     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-15 03:00:00"
  },
  {
     "dt":1534312800,
     "main":{
        "temp":294.05,
        "temp_min":294.05,
        "temp_max":294.05,
        "pressure":1021.27,
        "sea_level":1027.77,
        "grnd_level":1021.27,
        "humidity":100,
        "temp_kf":0
     },
     "weather":[
        {
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
        }
     ],
     "clouds":{
        "all":0
     },
     "wind":{
        "speed":2.46,
        "deg":274.504
     },
     "rain":{

     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-15 06:00:00"
  },
  {
     "dt":1534323600,
     "main":{
        "temp":293.495,
        "temp_min":293.495,
        "temp_max":293.495,
        "pressure":1021.36,
        "sea_level":1027.7,
        "grnd_level":1021.36,
        "humidity":100,
        "temp_kf":0
     },
     "weather":[
        {
           "id":800,
           "main":"Clear",
           "description":"clear sky",
           "icon":"01n"
        }
     ],
     "clouds":{
        "all":0
     },
     "wind":{
        "speed":3.01,
        "deg":277.505
     },
     "rain":{

     },
     "sys":{
        "pod":"n"
     },
     "dt_txt":"2018-08-15 09:00:00"
],
"city":{
  "id":4347778,
  "name":"Baltimore",
  "coord":{
     "lat":39.2909,
     "lon":-76.6108
  },
  "country":"US",
  "population":620961

} }

这是我尝试的JSON。我只是尝试提取:temp,min_temp,max_temp,天气主,描述和图标。 这是我尝试的JSON解析:

protected Weather[] doInBackground(String... params) {



    httpURL +=  "&q=" + cityName + apiKey;


    try {

        URL url = new URL(httpURL);


        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


        InputStreamReader inputStreamReader = new InputStreamReader(urlConnection.getInputStream());


        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);


        String inputLine;

        StringBuilder data = new StringBuilder();


        while ((inputLine = bufferedReader.readLine()) != null) {

            data.append(inputLine).append("\n");

        }


        bufferedReader.close();

        inputStreamReader.close();

        urlConnection.disconnect();


        Log.d("HTTP REQUEST", "SUCCESSFULLY COMMUNICATED WITH SERVER");


        return getResult(data.toString());

    } catch (Exception e) {

        Log.d("HTTP REQUEST", "FAILED TO RETRIEVE DATA FROM OPEN WEATHER");

        e.printStackTrace();

        return null;

    }

}


protected void onPostExecute(Weather result[]) {


}

public Weather[] getResult(String dataJSON) {

    if (dataJSON != null) {

        Log.d("HTTP REQUEST", dataJSON);

        Log.d("HTTP REQUEST", "SUCCESSFULLY RETRIEVED DATA FROM OPEN WEATHER ");


        try {

            JSONArray jsonRootArray = new JSONObject(dataJSON).getJSONArray("list");

            JSONObject jsonObject;

            JSONObject jsonObjectMain;

            JSONObject jsonObjectWeather;

            JSONObject jsonObjectTimeStamp;

            JSONArray jsonArrayWeather;


            Weather weatherData[] = new Weather[jsonRootArray.length()];


            for (int i = 0; i < jsonRootArray.length(); i++) {

                jsonObject = jsonRootArray.getJSONObject(i);

                jsonObjectMain = jsonObject.getJSONObject("main");

                String currentTemperature = jsonObjectMain.getString("temp");
                Double currentMinTemp = jsonObjectMain.getDouble("temp_min");
                Double currentMaxTemp = jsonObjectMain.getDouble("temp_max");
                Double currentHumidity = jsonObjectMain.getDouble("humidity");

                jsonObjectWeather = jsonObject.getJSONObject("weather");
                String weatherDescription = jsonObjectWeather.getString("description");
                String weatherIcon = jsonObjectWeather.getString("icon");

                String weatherTimeStamp = jsonObject.getString("dt_txt");


            }




            Log.d("JSON PARSE", "SUCCESSFULLY PARSED JSON");


            return weatherData;

        } catch (Exception e) {

            Log.d("JSON PARSE", "FAILED TO PARSE JSON");

            e.printStackTrace();

            return null;

        }

    } else {

        Log.d("GET RESULT", "COULD NOT GET RESULT, VARIABLE IS <<NULL>>");

        return null;

    }

}

这是我的List类。

公共课天气{

private final String location;
private final String currentTime;
private final Double currentTemp;
private final Double maxTemp;
private final Double minTemp;
private final Double humidity;
private final String weatherDescription;
private final String weatherIcon;
private final String weatherTimeStamp;

public Weather(String defaultLocation, String defaultCurrentTime, Double defaultCurrentTemp,
               Double defaultMaxTemp, Double defaultMinTemp, Double defaultHumidity,
               String defaultWeatherDescription, String defaultWeatherIcon, String defaultWeatherTimeStamp) {

    location = defaultLocation;
    currentTime= defaultCurrentTime;
    currentTemp = defaultCurrentTemp;
    maxTemp = defaultMaxTemp;
    minTemp = defaultMinTemp;
    humidity = defaultHumidity;
    weatherDescription = defaultWeatherDescription;
    weatherIcon = defaultWeatherIcon;
    weatherTimeStamp = defaultWeatherTimeStamp;


}

// Get default Location
public String getDefaultLocation() {
    return location;
}

// Get default weatherCondition
public String getDefaultCurrentTime() {
    return currentTime;
}

// Get default windSpeed
public Double getDefaultCurrentTemp() {
    return currentTemp;
}

// Get default Humidity
public Double getDefaultMaxTemp(){ return maxTemp; }

// Get default Humidity
public Double getDefaultMinTemp(){ return minTemp; }

// Get default Humidity
public Double getDefaultHumidity(){ return humidity; }

// Get default Humidity
public String getDefaultDescription(){ return weatherDescription; }

public String getDefaultWeatherTimeStamp(){return weatherTimeStamp; }

我的json解析正确吗?我认为它是正确的,但我不确定。

0 个答案:

没有答案