Reading JSON Response From URL in Java

Reading JSON Response From URL in Java:

As we know, JSON is becoming one of the famous and easily usable file type mainly due to its light weight. Before JSON, XML was preferred to transfer the data from one to another place. So being java developer it’s very mandatory to know how to read JSON files.

In this post I am covering how to read the JSON responses directly from URL (similar to web services) and also covered 3 different JSON files to help reading different JSON files. If you are facing any problem with any type of JSON reading, feel free to share in the below comments section to look and help you better.

 

Json Samples are taken from this link:

JSON Samples

 

Requirement:

json-simple-1.1.jar

 

Reading JSON Response From URL in Java Source Code:

 

package in.javadomain;

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

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ReadJsonFiles {
static ReadJsonFiles readJsonFiles = new ReadJsonFiles();

public static void main(String[] args) {
readJsonFiles.readColor1JsonFile();
System.out.println();
readJsonFiles.readColor2JsonFile();
System.out.println();
readJsonFiles.readColor3JsonFile();
}

private void readColor1JsonFile() {
System.out.println("First Json File Reading Started");
JSONObject jsonObj;
JSONArray jsonArr;
try {
jsonObj = readJsonFiles.readJsonFromUrl("http://demo.ngdeveloper.com/json_files/colors1.json");
jsonArr = (JSONArray) jsonObj.get("colorsArray");
for (int i = 0; i < jsonArr.size(); i++) {
System.out.println("Color Name => " + ((JSONObject) jsonArr.get(i)).get("colorName").toString());
System.out.println("Color Code => " + ((JSONObject) jsonArr.get(i)).get("hexValue").toString());
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("First Json File Reading Ended");
}

private void readColor3JsonFile() {
System.out.println("Third Json File Reading Started");
JSONObject jsonObj;
try {
jsonObj = readJsonFiles.readJsonFromUrl("http://demo.ngdeveloper.com/json_files/colors3.json");
Set<String> allJsonObjKeys = jsonObj.keySet();
for (String eachKey : allJsonObjKeys) {
System.out.println("Color Name => " + eachKey);
System.out.println("Color Code => " + jsonObj.get(eachKey));
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Third Json File Reading Ended");
}

private void readColor2JsonFile() {
System.out.println("Second Json File Reading Started");
JSONObject jsonObj;
JSONArray jsonArr;
try {
jsonObj = readJsonFiles.readJsonFromUrl("http://demo.ngdeveloper.com/json_files/colors2.json");
jsonArr = (JSONArray) jsonObj.get("colorsArray");
for (int i = 0; i < jsonArr.size(); i++) {
JSONObject jsonInnerObj = (JSONObject) jsonArr.get(i);
Set<String> allKeys = jsonInnerObj.keySet();
for (String jsonObjKey : allKeys) {
System.out.println("Color Name : " + jsonObjKey);
System.out.println("Color Code : " + ((JSONObject) jsonArr.get(i)).get(jsonObjKey));
}
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Second Json File Reading Ended");
}

private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

public JSONObject readJsonFromUrl(String url) throws IOException {
InputStream is = new URL(url).openStream();
JSONObject jsonObj = null;
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONParser parser = new JSONParser();
jsonObj = (JSONObject) parser.parse(jsonText);
} catch (ParseException e) {
e.printStackTrace();
} finally {
is.close();
}
return jsonObj;
}
}

 

Console Output:

 


First Json File Reading Started
Color Name => red
Color Code => #f00
Color Name => green
Color Code => #0f0
Color Name => blue
Color Code => #00f
Color Name => cyan
Color Code => #0ff
Color Name => magenta
Color Code => #f0f
Color Name => yellow
Color Code => #ff0
Color Name => black
Color Code => #000
First Json File Reading Ended

Second Json File Reading Started
Color Name : red
Color Code : #f00
Color Name : magenta
Color Code : #f0f
Color Name : green
Color Code : #0f0
Color Name : blue
Color Code : #00f
Color Name : yellow
Color Code : #ff0
Color Name : black
Color Code : #000
Color Name : cyan
Color Code : #0ff
Second Json File Reading Ended

Third Json File Reading Started
Color Name => red
Color Code => #f00
Color Name => magenta
Color Code => #f0f
Color Name => green
Color Code => #0f0
Color Name => blue
Color Code => #00f
Color Name => yellow
Color Code => #ff0
Color Name => black
Color Code => #000
Color Name => cyan
Color Code => #0ff
Third Json File Reading Ended

 

Thanks for reading this post. Feel free to write your comments/thoughts/suggestions below.

 

907 total views, 2 views today

About: Mirthbees

Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in