Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON from URL

Tags:

java

json

url

Is there any simplest way to parse JSON from a URL? I used Gson I can't find any helpful examples.

like image 413
Peril Avatar asked Sep 19 '11 07:09

Peril


People also ask

How can I get JSON data from URL?

Get JSON From URL Using jQuerygetJSON(url, data, success) is the signature method for getting JSON from an URL. In this case, the URL is a string that ensures the exact location of data, and data is just an object sent to the server. And if the request gets succeeded, the status comes through the success .

How do I parse JSON?

Use the JavaScript function JSON. parse() to convert text into a JavaScript object: const obj = JSON. parse('{"name":"John", "age":30, "city":"New York"}');


2 Answers

  1. First you need to download the URL (as text):

    private static String readUrl(String urlString) throws Exception {     BufferedReader reader = null;     try {         URL url = new URL(urlString);         reader = new BufferedReader(new InputStreamReader(url.openStream()));         StringBuffer buffer = new StringBuffer();         int read;         char[] chars = new char[1024];         while ((read = reader.read(chars)) != -1)             buffer.append(chars, 0, read);           return buffer.toString();     } finally {         if (reader != null)             reader.close();     } } 
  2. Then you need to parse it (and here you have some options).

    • GSON (full example):

      static class Item {     String title;     String link;     String description; }  static class Page {     String title;     String link;     String description;     String language;     List<Item> items; }  public static void main(String[] args) throws Exception {      String json = readUrl("http://www.javascriptkit.com/"                           + "dhtmltutors/javascriptkit.json");      Gson gson = new Gson();             Page page = gson.fromJson(json, Page.class);      System.out.println(page.title);     for (Item item : page.items)         System.out.println("    " + item.title); } 

      Outputs:

      javascriptkit.com     Document Text Resizer     JavaScript Reference- Keyboard/ Mouse Buttons Events     Dynamically loading an external JavaScript or CSS file 
    • Try the java API from json.org:

      try {     JSONObject json = new JSONObject(readUrl("..."));      String title = (String) json.get("title");     ...  } catch (JSONException e) {     e.printStackTrace(); } 
like image 58
dacwe Avatar answered Sep 26 '22 04:09

dacwe


GSON has a builder that takes a Reader object: fromJson(Reader json, Class classOfT).

This means you can create a Reader from a URL and then pass it to Gson to consume the stream and do the deserialisation.

Only three lines of relevant code.

import java.io.InputStreamReader; import java.net.URL; import java.util.Map;  import com.google.gson.Gson;  public class GsonFetchNetworkJson {      public static void main(String[] ignored) throws Exception {          URL url = new URL("https://httpbin.org/get?color=red&shape=oval");         InputStreamReader reader = new InputStreamReader(url.openStream());         MyDto dto = new Gson().fromJson(reader, MyDto.class);          // using the deserialized object         System.out.println(dto.headers);         System.out.println(dto.args);         System.out.println(dto.origin);         System.out.println(dto.url);     }      private class MyDto {         Map<String, String> headers;         Map<String, String> args;         String origin;         String url;     } } 

If you happen to get a 403 error code with an endpoint which otherwise works fine (e.g. with curl or other clients) then a possible cause could be that the endpoint expects a User-Agent header and by default Java URLConnection is not setting it. An easy fix is to add at the top of the file e.g. System.setProperty("http.agent", "Netscape 1.0");.

like image 34
ccpizza Avatar answered Sep 24 '22 04:09

ccpizza