Skip to content

Add examples for How to parse json in java blog #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions java-json/parse-json-in-java/ParseJSONUsingGSON.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
67 changes: 67 additions & 0 deletions java-json/parse-json-in-java/ParseJSONUsingJSONJava.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
73 changes: 73 additions & 0 deletions java-json/parse-json-in-java/ParseJSONUsingJsonSimple.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
43 changes: 43 additions & 0 deletions java-json/parse-json-in-java/pom.xml
Original file line number Diff line number Diff line change
@@ -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>