0

What would be the most basic way to have Java simply print out data from a JSON URL? I just want to be able to call a simple print statement on it if possible.

Here is a link to the API I am trying to pull data from: Github - PHP-MPOS API Reference

Thanks!

SirFerret
  • 61
  • 1
  • 8

2 Answers2

2

I think Netflix's feign is one of the simplest java http clients out there: https://github.com/Netflix/feign

If you don't want to use such libs and just want to build something which works, check this out: HTTP Json requests in Java?

Community
  • 1
  • 1
zengr
  • 38,346
  • 37
  • 130
  • 192
0

Here is simple example where I fetch json from url and print out:

public static void main(String[] args) throws MalformedURLException, IOException {
    String sURL = "your_url_goes_here";
    URL url = new URL(sURL);
    HttpURLConnection request = (HttpURLConnection) url.openConnection();
    request.connect();

    // Convert to a JSON object to print data
    JsonParser jp = new JsonParser(); //from gson
    //Convert the input stream to a json element
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); 
    JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object. 

    System.out.println(rootobj.toString()); // json is printed
}
Gabit Kemelov
  • 106
  • 1
  • 8