0

I created an ArrayList with the json values from an Rest API.

This is the code to read the Rest API:

@RestController
public class exemploclass {
    
    @RequestMapping(value="/vectors")
    //@Scheduled(fixedRate = 5000)
    public ArrayList<StateVector> getStateVectors() throws Exception {
        
        ArrayList<StateVector> vectors = new ArrayList<>();
        
        String url = "https://opensky-network.org/api/states/all?lamin=41.1&lomin=6.1&lamax=43.1&lomax=8.1";
        //String url = "https://opensky-network.org/api/states/all?lamin=45.8389&lomin=5.9962&lamax=47.8229&lomax=10.5226";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        // optional default is GET
        con.setRequestMethod("GET");
        //add request header
        con.setRequestProperty("User-Agent", "Mozilla/5.0");
        int responseCode = con.getResponseCode();
        System.out.println("\nSending 'GET' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);
        BufferedReader in = new BufferedReader(
                new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
        }

        in.close();
        
        
        JSONObject myResponse = new JSONObject(response.toString());
        JSONArray states = myResponse.getJSONArray("states");
        System.out.println("result after Reading JSON Response");
            
        for (int i = 0; i < states.length(); i++) {
            
            JSONArray jsonVector = states.getJSONArray(i);
            String icao24 = jsonVector.optString(0);
            String callsign = jsonVector.optString(1);
            String origin_country = jsonVector.optString(2);
            Boolean on_ground = jsonVector.optBoolean(8);
            
            //System.out.println("icao24: " + icao24 + "| callsign: " + callsign + "| origin_country: " + origin_country + "| on_ground: " + on_ground);
            //System.out.println("\n");
            
            StateVector sv = new StateVector(icao24, callsign, origin_country, on_ground);
            vectors.add(sv);
  
        }
        
        System.out.println("Size of data: " + vectors.size());

        return vectors;
        
    }

}

The last line " return vectors;" returns a list with the values i parsed and returns it like this: enter image description here

But i want this more "pretty", i want it to be one Array in each line, how can i achieve this?

P.S. Its on the .html page, not on console

7
  • Your vectors is just a java.util.ArrayList<E> and it's printed with its .toString() method; however, that's not really important for any HTTP client.. it's your visual concern to see the text in a formatted way.. and in reality, that's going to be the raw text data. What's important is not to forged the appropriate Content-type header. Commented Mar 29, 2021 at 15:09
  • But i dont want to print it in my console, its on the .html page, how can i do it there? Commented Mar 29, 2021 at 15:10
  • Your want to print JSON in your html page? Commented Mar 29, 2021 at 15:11
  • Its already printing there, the image its on the .html, but now i want it to be a line each time instead of everything on the same line Commented Mar 29, 2021 at 15:12
  • Does this answer your question? Pretty-Print JSON in Java Commented Mar 29, 2021 at 15:26

1 Answer 1

1

Your return value seems a valid Json Object. If you want it more pretty so you can read it clearly then pass it through an application that makes that json pretty.

If you call your API from Postman, it will give you a pretty Json Object which will be better formatted. This will be because you have annotated your controller with @RestController so it will deliver an application/json response which Postman will know and then it will try to make it prettier.

P.S. Its on the .html page, not on console

So you hit your API from a browser. Most browsers don't expect a Json object to be returned, so they will not make it pretty. You can't force that from your Service either.

Just hit your API from Postman, it will understand it and make it pretty.

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

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.