Home > Mobile >  How to I convert stringified json to object in java?
How to I convert stringified json to object in java?

Time:11-05

My string is an API response from OpenWeatherMap. I'm trying to print the info from the API response, but the data printed in the terminal is messy and unstructured.

**edit: Is it possible to do it without third party dependency such as GSON?

My code looks a little something like this

public Void apply(Void aVoid) {
                try {

                    URL obj = new URL("https://api.openweathermap.org/data/2.5/weather?lat=44.34&lon=10.99&appid={API key}");
                    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                    con.setRequestMethod("GET");
                    int responseCode = con.getResponseCode();
                    System.out.println("GET Response Code :: "   responseCode);
                    if (responseCode == HttpURLConnection.HTTP_OK) { // success
                        BufferedReader in = new BufferedReader(new InputStreamReader(
                        con.getInputStream()));
                        String inputLine;
                        StringBuffer response = new StringBuffer();

                        while ((inputLine = in.readLine()) != null) {
                           response.append(inputLine);
                        }
                        in.close();
                        // print result
                        System.out.println(response.toString());
                        
                    } else {
                        System.out.println("GET request not worked");
                    }
                }
                catch (Exception e) {
                    System.out.println(e.getMessage());
                }
                return null;
            }

This is a preview of what is printed in my terminal.

{"coord":{"lon":10.99,"lat":44.34},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04d"}],"base":"stations","main":{"temp":281.76,"feels_like":280.46,"temp_min":279.79,"temp_max":283.19,"pressure":1011,"humidity":69,"sea_level":1011,"grnd_level":925},"visibility":10000,"wind":{"speed":2.32,"deg":333,"gust":5.27},"clouds":{"all":100},"dt":1667633869,"sys":{"type":2,"id":2075663,"country":"IT","sunrise":1667627890,"sunset":1667664063},"timezone":3600,"id":3163858,"name":"Zocca","cod":200}

I expected it to be formatted properly like this.


{
  "coord": {
    "lon": 10.99,
    "lat": 44.34
  },
  "weather": [
    {
      "id": 501,
      "main": "Rain",
      "description": "moderate rain",
      "icon": "10d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 298.48,
    "feels_like": 298.74,
    "temp_min": 297.56,
    "temp_max": 300.05,
    "pressure": 1015,
    "humidity": 64,
    "sea_level": 1015,
    "grnd_level": 933
  },
  "visibility": 10000,
  "wind": {
    "speed": 0.62,
    "deg": 349,
    "gust": 1.18
  },
  "rain": {
    "1h": 3.16
  },
  "clouds": {
    "all": 100
  },
  "dt": 1661870592,
  "sys": {
    "type": 2,
    "id": 2075663,
    "country": "IT",
    "sunrise": 1661834187,
    "sunset": 1661882248
  },
  "timezone": 7200,
  "id": 3163858,
  "name": "Zocca",
  "cod": 200
}                        

              

CodePudding user response:

There are several libraries you can use to pretty print JSON in Java, like GSON or org.JSON. See this post.

  • Related