Skip to content

Commit 25f7105

Browse files
authored
Merge pull request #4 from coderolls/parse-json-in-java
Add examples for How to parse json in java blog
2 parents 9df3278 + 327c5cc commit 25f7105

File tree

4 files changed

+251
-0
lines changed

4 files changed

+251
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.coderolls.JSONExample;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonObject;
6+
7+
/**
8+
* A program to parse JSON strin in Java using Gson
9+
* @author Gaurav Kukade at coderolls.com
10+
*/
11+
12+
public class ParseJSONUsingGSON {
13+
14+
public static void main(String[] args) {
15+
16+
//take json as string
17+
String jsonString = "{"
18+
+ " \"name\": \"coderolls\","
19+
+ " \"type\": \"blog\","
20+
+ " \"address\": {"
21+
+ " \"street\": \"1600 Pennsylvania Avenue NW\","
22+
+ " \"city\": \"Washington\","
23+
+ " \"state\": \"DC\""
24+
+ " },"
25+
+ " \"employees\": ["
26+
+ " {"
27+
+ " \"firstName\": \"John\","
28+
+ " \"lastName\": \"Doe\""
29+
+ " },"
30+
+ " {"
31+
+ " \"firstName\": \"Anna\","
32+
+ " \"lastName\": \"Smith\""
33+
+ " },"
34+
+ " {"
35+
+ " \"firstName\": \"Peter\","
36+
+ " \"lastName\": \"Jones\""
37+
+ " }"
38+
+ " ]"
39+
+ "}";
40+
41+
System.out.println("Parsing the json string in java using Gson......\n");
42+
Gson gson = new Gson();
43+
44+
//get json object from the json string
45+
JsonObject coderollsJsonObject = gson.fromJson(jsonString, JsonObject.class);
46+
47+
//now we can access the values
48+
String name = coderollsJsonObject.get("name").getAsString();
49+
System.out.println("Name: "+name+"\n");
50+
51+
//we can get the JSON object present as value of any key in the parent JSON
52+
JsonObject addressJsonObject = coderollsJsonObject.get("address").getAsJsonObject();
53+
54+
//access the values of the addressJSONObject
55+
String street = addressJsonObject.get("street").getAsString();
56+
System.out.println("Street: "+street+"\n");
57+
58+
59+
//we can get the json array present as value of any key in the parent JSON
60+
JsonArray employeesJsonArray = coderollsJsonObject.get("employees").getAsJsonArray();
61+
System.out.println("Printing the employess json array: \n"+employeesJsonArray.toString()+"\n");
62+
63+
//we can get individual json object at an index from the employeesJSONArray
64+
JsonObject employeeJsonObject = employeesJsonArray.get(0).getAsJsonObject();
65+
String firstName = employeeJsonObject.get("firstName").getAsString();
66+
System.out.println("First Name of the employee at index 0: "+firstName);
67+
}
68+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.coderolls.JSONExample;
2+
3+
import org.json.JSONArray;
4+
import org.json.JSONObject;
5+
6+
/**
7+
* A program to parse JSON strin in Java using json-simple
8+
* @author Gaurav Kukade at coderolls.com
9+
*/
10+
11+
public class ParseJSONUsingJSONJava {
12+
13+
public static void main(String[] args) {
14+
15+
//take json as string
16+
String jsonString = "{"
17+
+ " \"name\": \"coderolls\","
18+
+ " \"type\": \"blog\","
19+
+ " \"address\": {"
20+
+ " \"street\": \"1600 Pennsylvania Avenue NW\","
21+
+ " \"city\": \"Washington\","
22+
+ " \"state\": \"DC\""
23+
+ " },"
24+
+ " \"employees\": ["
25+
+ " {"
26+
+ " \"firstName\": \"John\","
27+
+ " \"lastName\": \"Doe\""
28+
+ " },"
29+
+ " {"
30+
+ " \"firstName\": \"Anna\","
31+
+ " \"lastName\": \"Smith\""
32+
+ " },"
33+
+ " {"
34+
+ " \"firstName\": \"Peter\","
35+
+ " \"lastName\": \"Jones\""
36+
+ " }"
37+
+ " ]"
38+
+ "}";
39+
40+
System.out.println("Parsing the json string in java using JSON-Java......\n");
41+
42+
//add jsonString to the constructor
43+
JSONObject coderollsJSONObject = new JSONObject(jsonString);
44+
45+
//now we can access the values
46+
String name = coderollsJSONObject.getString("name");
47+
System.out.println("Name: "+name+"\n");
48+
49+
//we can get the JSON object present as value of any key in the parent JSON
50+
JSONObject addressJSONObject = coderollsJSONObject.getJSONObject("address");
51+
52+
//access the values of the addressJSONObject
53+
String street = addressJSONObject.getString("street");
54+
System.out.println("Street: "+street+"\n");
55+
56+
57+
//we can get the json array present as value of any key in the parent JSON
58+
JSONArray employeesJSONArray = coderollsJSONObject.getJSONArray("employees");
59+
System.out.println("Printing the employess json array: \n"+employeesJSONArray.toString()+"\n");
60+
61+
//we can get individual json object at an index from the employeesJSONArray
62+
JSONObject employeeJSONObject = employeesJSONArray.getJSONObject(0);
63+
String firstName = employeeJSONObject.getString("firstName");
64+
System.out.println("First Name of the employee at index 0: "+firstName);
65+
}
66+
67+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.coderolls.JSONExample;
2+
3+
import org.json.simple.JSONArray;
4+
import org.json.simple.JSONObject;
5+
import org.json.simple.parser.JSONParser;
6+
import org.json.simple.parser.ParseException;
7+
8+
/**
9+
* A program to parse JSON strin in Java using json-simple
10+
* @author Gaurav Kukade at coderolls.com
11+
*/
12+
13+
public class ParseJSONUsingJsonSimple {
14+
15+
public static void main(String[] args) {
16+
//take json as string
17+
String jsonString = "{"
18+
+ " \"name\": \"coderolls\","
19+
+ " \"type\": \"blog\","
20+
+ " \"address\": {"
21+
+ " \"street\": \"1600 Pennsylvania Avenue NW\","
22+
+ " \"city\": \"Washington\","
23+
+ " \"state\": \"DC\""
24+
+ " },"
25+
+ " \"employees\": ["
26+
+ " {"
27+
+ " \"firstName\": \"John\","
28+
+ " \"lastName\": \"Doe\""
29+
+ " },"
30+
+ " {"
31+
+ " \"firstName\": \"Anna\","
32+
+ " \"lastName\": \"Smith\""
33+
+ " },"
34+
+ " {"
35+
+ " \"firstName\": \"Peter\","
36+
+ " \"lastName\": \"Jones\""
37+
+ " }"
38+
+ " ]"
39+
+ "}";
40+
41+
System.out.println("Parsing the json string in java using json-simple......\n");
42+
43+
JSONParser parser = new JSONParser();
44+
JSONObject coderollsJSONObject = new JSONObject();
45+
try {
46+
coderollsJSONObject = (JSONObject) parser.parse(jsonString);
47+
} catch (ParseException e) {
48+
e.printStackTrace();
49+
}
50+
51+
//now we can access the values
52+
String name = (String) coderollsJSONObject.get("name");
53+
System.out.println("Name: "+name+"\n");
54+
55+
//we can get the JSON object present as value of any key in the parent JSON
56+
JSONObject addressJSONObject = (JSONObject) coderollsJSONObject.get("address");
57+
58+
//access the values of the addressJSONObject
59+
String street = (String) addressJSONObject.get("street");
60+
System.out.println("Street: "+street+"\n");
61+
62+
63+
//we can get the json array present as value of any key in the parent JSON
64+
JSONArray employeesJSONArray = (JSONArray) coderollsJSONObject.get("employees");
65+
System.out.println("Printing the employess json array: \n"+employeesJSONArray.toString()+"\n");
66+
67+
//we can get individual json object at an index from the employeesJSONArray
68+
JSONObject employeeJSONObject = (JSONObject) employeesJSONArray.get(0);
69+
String firstName = (String) employeeJSONObject.get("firstName");
70+
System.out.println("First Name of the employee at index 0: "+firstName);
71+
}
72+
73+
}

java-json/parse-json-in-java/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.coderolls</groupId>
6+
<artifactId>JSONExample</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>JSONExample</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>3.8.1</version>
22+
<scope>test</scope>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.json</groupId>
26+
<artifactId>json</artifactId>
27+
<version>20201115</version>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.google.code.gson</groupId>
31+
<artifactId>gson</artifactId>
32+
<version>2.8.6</version>
33+
</dependency>
34+
35+
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
36+
<dependency>
37+
<groupId>com.googlecode.json-simple</groupId>
38+
<artifactId>json-simple</artifactId>
39+
<version>1.1.1</version>
40+
</dependency>
41+
42+
</dependencies>
43+
</project>

0 commit comments

Comments
 (0)