From 327c5cc5f4e17cc8317bbe5fde7a0682fc42837c Mon Sep 17 00:00:00 2001
From: Gaurav <gkukade54@gmail.com>
Date: Sat, 9 Jan 2021 10:56:40 +0530
Subject: [PATCH] Add examples for How to parse json in java blog

---
 .../ParseJSONUsingGSON.java                   | 68 +++++++++++++++++
 .../ParseJSONUsingJSONJava.java               | 67 +++++++++++++++++
 .../ParseJSONUsingJsonSimple.java             | 73 +++++++++++++++++++
 java-json/parse-json-in-java/pom.xml          | 43 +++++++++++
 4 files changed, 251 insertions(+)
 create mode 100644 java-json/parse-json-in-java/ParseJSONUsingGSON.java
 create mode 100644 java-json/parse-json-in-java/ParseJSONUsingJSONJava.java
 create mode 100644 java-json/parse-json-in-java/ParseJSONUsingJsonSimple.java
 create mode 100644 java-json/parse-json-in-java/pom.xml

diff --git a/java-json/parse-json-in-java/ParseJSONUsingGSON.java b/java-json/parse-json-in-java/ParseJSONUsingGSON.java
new file mode 100644
index 0000000..0322ce2
--- /dev/null
+++ b/java-json/parse-json-in-java/ParseJSONUsingGSON.java
@@ -0,0 +1,68 @@
+package com.coderolls.JSONExample;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+
+/**
+ * A program to parse JSON strin in Java using Gson
+ * @author Gaurav Kukade at coderolls.com
+ */
+
+public class ParseJSONUsingGSON {
+
+	public static void main(String[] args) {
+
+		//take json as string
+		String jsonString = "{"
+				+ "  \"name\": \"coderolls\","
+				+ "  \"type\": \"blog\","
+				+ "  \"address\": {"
+				+ "    \"street\": \"1600 Pennsylvania Avenue NW\","
+				+ "    \"city\": \"Washington\","
+				+ "    \"state\": \"DC\""
+				+ "  },"
+				+ "  \"employees\": ["
+				+ "    {"
+				+ "      \"firstName\": \"John\","
+				+ "      \"lastName\": \"Doe\""
+				+ "    },"
+				+ "    {"
+				+ "      \"firstName\": \"Anna\","
+				+ "      \"lastName\": \"Smith\""
+				+ "    },"
+				+ "    {"
+				+ "      \"firstName\": \"Peter\","
+				+ "      \"lastName\": \"Jones\""
+				+ "    }"
+				+ "  ]"
+				+ "}";
+		
+		System.out.println("Parsing the json string in java using Gson......\n");
+		Gson gson = new Gson();
+		
+		//get json object from the json string
+		JsonObject coderollsJsonObject = gson.fromJson(jsonString, JsonObject.class);
+		
+		//now we can access the values 
+		String name = coderollsJsonObject.get("name").getAsString();
+		System.out.println("Name: "+name+"\n");
+		
+		//we can get the JSON object present as value of any key in the parent JSON
+		JsonObject addressJsonObject = coderollsJsonObject.get("address").getAsJsonObject();
+		
+		//access the values of the addressJSONObject 
+		String street = addressJsonObject.get("street").getAsString();
+		System.out.println("Street: "+street+"\n");
+		
+		
+		//we can get the json array present as value of any key in the parent JSON
+		JsonArray employeesJsonArray = coderollsJsonObject.get("employees").getAsJsonArray();
+		System.out.println("Printing the employess json array: \n"+employeesJsonArray.toString()+"\n");
+		
+		//we can get individual json object at an index from the employeesJSONArray
+		JsonObject employeeJsonObject = employeesJsonArray.get(0).getAsJsonObject();
+		String firstName = employeeJsonObject.get("firstName").getAsString();
+		System.out.println("First Name of the employee at index 0: "+firstName);
+	}
+}
diff --git a/java-json/parse-json-in-java/ParseJSONUsingJSONJava.java b/java-json/parse-json-in-java/ParseJSONUsingJSONJava.java
new file mode 100644
index 0000000..48de8e0
--- /dev/null
+++ b/java-json/parse-json-in-java/ParseJSONUsingJSONJava.java
@@ -0,0 +1,67 @@
+package com.coderolls.JSONExample;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+/**
+ * A program to parse JSON strin in Java using json-simple
+ * @author Gaurav Kukade at coderolls.com
+ */
+
+public class ParseJSONUsingJSONJava {
+
+	public static void main(String[] args) {
+		
+		//take json as string
+		String jsonString = "{"
+				+ "  \"name\": \"coderolls\","
+				+ "  \"type\": \"blog\","
+				+ "  \"address\": {"
+				+ "    \"street\": \"1600 Pennsylvania Avenue NW\","
+				+ "    \"city\": \"Washington\","
+				+ "    \"state\": \"DC\""
+				+ "  },"
+				+ "  \"employees\": ["
+				+ "    {"
+				+ "      \"firstName\": \"John\","
+				+ "      \"lastName\": \"Doe\""
+				+ "    },"
+				+ "    {"
+				+ "      \"firstName\": \"Anna\","
+				+ "      \"lastName\": \"Smith\""
+				+ "    },"
+				+ "    {"
+				+ "      \"firstName\": \"Peter\","
+				+ "      \"lastName\": \"Jones\""
+				+ "    }"
+				+ "  ]"
+				+ "}";
+		
+		System.out.println("Parsing the json string in java using JSON-Java......\n");
+
+		//add jsonString to the constructor
+		JSONObject coderollsJSONObject = new JSONObject(jsonString);
+		
+		//now we can access the values 
+		String name = coderollsJSONObject.getString("name");
+		System.out.println("Name: "+name+"\n");
+		
+		//we can get the JSON object present as value of any key in the parent JSON
+		JSONObject addressJSONObject = coderollsJSONObject.getJSONObject("address");
+		
+		//access the values of the addressJSONObject 
+		String street = addressJSONObject.getString("street");
+		System.out.println("Street: "+street+"\n");
+		
+		
+		//we can get the json array present as value of any key in the parent JSON
+		JSONArray employeesJSONArray = coderollsJSONObject.getJSONArray("employees");
+		System.out.println("Printing the employess json array: \n"+employeesJSONArray.toString()+"\n");
+		
+		//we can get individual json object at an index from the employeesJSONArray
+		JSONObject employeeJSONObject = employeesJSONArray.getJSONObject(0);
+		String firstName = employeeJSONObject.getString("firstName");
+		System.out.println("First Name of the employee at index 0: "+firstName);
+	}
+
+}
diff --git a/java-json/parse-json-in-java/ParseJSONUsingJsonSimple.java b/java-json/parse-json-in-java/ParseJSONUsingJsonSimple.java
new file mode 100644
index 0000000..5c3e2e2
--- /dev/null
+++ b/java-json/parse-json-in-java/ParseJSONUsingJsonSimple.java
@@ -0,0 +1,73 @@
+package com.coderolls.JSONExample;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+
+/**
+ * A program to parse JSON strin in Java using json-simple
+ * @author Gaurav Kukade at coderolls.com
+ */
+
+public class ParseJSONUsingJsonSimple {
+
+	public static void main(String[] args) {
+		//take json as string
+		String jsonString = "{"
+						+ "  \"name\": \"coderolls\","
+						+ "  \"type\": \"blog\","
+						+ "  \"address\": {"
+						+ "    \"street\": \"1600 Pennsylvania Avenue NW\","
+						+ "    \"city\": \"Washington\","
+						+ "    \"state\": \"DC\""
+						+ "  },"
+						+ "  \"employees\": ["
+						+ "    {"
+						+ "      \"firstName\": \"John\","
+						+ "      \"lastName\": \"Doe\""
+						+ "    },"
+						+ "    {"
+						+ "      \"firstName\": \"Anna\","
+						+ "      \"lastName\": \"Smith\""
+						+ "    },"
+						+ "    {"
+						+ "      \"firstName\": \"Peter\","
+						+ "      \"lastName\": \"Jones\""
+						+ "    }"
+						+ "  ]"
+						+ "}";
+				
+		System.out.println("Parsing the json string in java using json-simple......\n");
+		
+		JSONParser parser = new JSONParser();
+		JSONObject coderollsJSONObject = new JSONObject();
+		try {
+			coderollsJSONObject = (JSONObject) parser.parse(jsonString);
+		} catch (ParseException e) {
+			e.printStackTrace();
+		}
+		
+		//now we can access the values 
+		String name = (String) coderollsJSONObject.get("name");
+		System.out.println("Name: "+name+"\n");
+		
+		//we can get the JSON object present as value of any key in the parent JSON
+		JSONObject addressJSONObject = (JSONObject) coderollsJSONObject.get("address");
+		
+		//access the values of the addressJSONObject 
+		String street = (String) addressJSONObject.get("street");
+		System.out.println("Street: "+street+"\n");
+		
+		
+		//we can get the json array present as value of any key in the parent JSON
+		JSONArray employeesJSONArray = (JSONArray) coderollsJSONObject.get("employees");
+		System.out.println("Printing the employess json array: \n"+employeesJSONArray.toString()+"\n");
+		
+		//we can get individual json object at an index from the employeesJSONArray
+		JSONObject employeeJSONObject = (JSONObject) employeesJSONArray.get(0);
+		String firstName = (String) employeeJSONObject.get("firstName");
+		System.out.println("First Name of the employee at index 0: "+firstName);
+	}
+
+}
diff --git a/java-json/parse-json-in-java/pom.xml b/java-json/parse-json-in-java/pom.xml
new file mode 100644
index 0000000..3bcf581
--- /dev/null
+++ b/java-json/parse-json-in-java/pom.xml
@@ -0,0 +1,43 @@
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>com.coderolls</groupId>
+  <artifactId>JSONExample</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>jar</packaging>
+
+  <name>JSONExample</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+	    <groupId>org.json</groupId>
+	    <artifactId>json</artifactId>
+	    <version>20201115</version>
+	</dependency>
+	<dependency>
+	    <groupId>com.google.code.gson</groupId>
+	    <artifactId>gson</artifactId>
+    	<version>2.8.6</version>
+	</dependency>
+	
+	<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
+	<dependency>
+	    <groupId>com.googlecode.json-simple</groupId>
+	    <artifactId>json-simple</artifactId>
+	    <version>1.1.1</version>
+	</dependency>
+	
+  </dependencies>
+</project>
