Posted In: JSON

Ways to read json from a URL in Java – Using java.net.URL

 

1. Using java.net.URL and org.apache.commons.io.IOUtils

 

MAVEN

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-io</artifactId>
	<version>1.3.2</version>
</dependency>
<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.4</version>
	<classifier>jdk15</classifier>
</dependency>
package com.javausecase.jsonread;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.apache.commons.io.IOUtils;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class CallJsonApi1 {
	public static JSONObject callJsonApi(String url)
	        throws IOException, JSONException {
		InputStream is = new URL(url).openStream();
		try {
			String jsonText = IOUtils.toString(is, "UTF-8");
			JSONObject json = JSONObject.fromObject(jsonText);
			System.out.println(json.toString(4));
			return json;
		} finally {
			is.close();
		}
	}

	public static void main(String[] args)
	        throws IOException, JSONException {
		callJsonApi(
		        "https://jsonplaceholder.typicode.com/posts/1");
	}
}

2. Using java.net.URL and com.google.common.io.CharStreams

 

MAVEN

<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>21.0</version>
</dependency>
<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.4</version>
	<classifier>jdk15</classifier>
</dependency>
package com.javausecase.jsonread;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import com.google.common.io.CharStreams;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class CallJsonApi2 {
	public static JSONObject callJsonApi(String url)
	        throws IOException, JSONException {
		InputStream is = null;
		try {
			is = new URL(url).openStream();
			InputStreamReader inr = new InputStreamReader(is);			
			String jsonText = CharStreams.toString(inr);
			JSONObject json = JSONObject.fromObject(jsonText);
			System.out.println(json.toString(4));
			return json;
		} finally {
			is.close();
		}
	}

	public static void main(String[] args)
	        throws IOException, JSONException {
		callJsonApi(
		        "https://jsonplaceholder.typicode.com/posts/1");
	}
}

3. Using java.net.URL and java.io.BufferedReader

 

MAVEN

<dependency>
	<groupId>net.sf.json-lib</groupId>
	<artifactId>json-lib</artifactId>
	<version>2.4</version>
	<classifier>jdk15</classifier>
</dependency>
package com.javausecase.jsonread;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;

import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class CallJsonApi3 {
	public static JSONObject callJsonApi(String url)
	        throws IOException, JSONException {
		InputStream is = null;
		StringBuilder jsonText = new StringBuilder();
		try {
			is = new URL(url).openStream();
			BufferedReader brd = new BufferedReader(
			        new InputStreamReader(is,
			                Charset.forName("UTF-8")));
			int cp;
			while ((cp = brd.read()) != -1) {
				jsonText.append((char) cp);
			}

			JSONObject json = JSONObject
			        .fromObject(jsonText.toString());
			System.out.println(json.toString(4));
			return json;
		} finally {
			is.close();
		}
	}

	public static void main(String[] args)
	        throws IOException, JSONException {
		callJsonApi(
		        "https://jsonplaceholder.typicode.com/posts/1");
	}
}

Tags: ,

by , on March 5th, 2017

  • Categories