0

Can't figure out how to get string of JSON response. From OpenWeatherMap API ( https://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b6907d289e10d714a6e88b30761fae22 ) want to get the first temp_min and temp_max.

I have tried to post the JSON response in a JSON formatter and go through it logically but that didn't help me much. Tried different solutions throughout google.

The last try from my side is this:

JSONObject jsonObject = new JSONObject(response);
JSONArray array = jsonObject.getJSONArray("list");
JSONObject firstObject = (JSONObject)array.get(0);
String tempmax = firstObject.getJSONObject("main").getString("temp_max");
String tempmin = firstObject.getJSONObject("main").getString("temp_min");

From the following API response, I want to receive temp_min and temp_max:

{  
   "cod":"200",
   "message":0.0032,
   "cnt":36,
   "list":[  
      {  
         "dt":1487246400,
         "main":{  
            "temp":286.67,
            "temp_min":281.556,
            "temp_max":286.67,
            "pressure":972.73,
            "sea_level":1046.46,
            "grnd_level":972.73,
            "humidity":75,
            "temp_kf":5.11
         },
         "weather":[  ],
         "clouds":{  },
         "wind":{  },
         "sys":{  },
         "dt_txt":"2017-02-16 12:00:00"
      },
[..]

I expect to get the temp_min and temp_max values from API response, but at the moment it's just empty.

2
  • I'm not getting an error, just the variables tempmax and tempmin are empty (blank), but I want that both values are read from the API request.In this case it should be like: tempmax = 286.67 tempmin = 281.556 Commented Apr 20, 2019 at 14:36
  • You might have a casting issue. Try cast this line to String (String)firstObject.getJSONObject("main").getString("temp_max"); I also suggest to add try catch to your code as if there is an error you are not capturing it Commented Apr 20, 2019 at 14:53

2 Answers 2

1

I have just checked your code using this library with newest version. I loaded json content from local file and I had to change the way you read values of temperature :

    public static void main(String[] args) throws IOException {
        String collect = Files.lines(Paths.get("src/main/resources/waether.json")).collect(Collectors.joining());

        JSONObject jsonObject = new JSONObject(collect);
        JSONArray array = jsonObject.getJSONArray("list");
        JSONObject firstObject = (JSONObject)array.get(0);
        double tempmax = firstObject.getJSONObject("main").getDouble("temp_max");
        double tempmin = firstObject.getJSONObject("main").getDouble("temp_min");

        System.out.println("Temp min " + tempmin);
        System.out.println("Temp max " + tempmax);
    }

The output is :

Temp min 259.086
Temp max 261.45

As you see I had to use getDouble methods as those values were not json strings - they were numbers. I am not sure which version of this library you are using but with newest version it works.

Sign up to request clarification or add additional context in comments.

Comments

0

Big thanks to michalk and others for help. Edited michalk's answer a bit, so it meets my project and it solved the problem I was having.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.