JSON.simple – How to parse JSON
By:Roy.LiuLast updated:2019-08-11
In this tutorial, we will show you how to parse JSON with JSON.simple
JSON.simple short history
This project was formerly JSON.simple 1.x from a Google code project by Yidong, now maintaining by Clifton Labs, read this JSON.simple history
This project was formerly JSON.simple 1.x from a Google code project by Yidong, now maintaining by Clifton Labs, read this JSON.simple history
P.S Tested with json-simple 3.1.0
1. Download JSON.simple
pom.xml
<dependency> <groupId>com.github.cliftonlabs</groupId> <artifactId>json-simple</artifactId> <version>3.1.0</version> </dependency>
2. POJO + Jsonable
2.1 To convert Java objects to/from JSON, The JSON.simple requires POJO to implement Jsonable, and override toJson()
Staff.java
package com.mkyong; import com.github.cliftonlabs.json_simple.JsonObject; import com.github.cliftonlabs.json_simple.Jsonable; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.math.BigDecimal; import java.util.Arrays; import java.util.List; import java.util.Map; public class Staff implements Jsonable { private String name; private int age; private String[] position; private List<String> skills; private Map<String, BigDecimal> salary; //..getters setters @Override public String toJson() { final StringWriter writable = new StringWriter(); try { this.toJson(writable); } catch (final IOException e) { return writable.toString(); @Override public void toJson(Writer writer) throws IOException { final JsonObject json = new JsonObject(); json.put("name", this.getName()); json.put("age", this.getAge()); json.put("position", this.getPosition()); json.put("skills", this.getSkills()); json.put("salary", this.getSalary()); json.toJson(writer);
3. Java objects to JSON
3.1 Convert a Java object to JSON string and save as .json file.
JsonSimple1.java
package com.mkyong; import com.github.cliftonlabs.json_simple.Jsoner; import java.io.FileWriter; import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class JsonSimple1 { public static void main(String[] args) throws IOException { Staff staff = createStaff(); // Java objects to JSON String String json = Jsoner.serialize(staff); // pretty print json = Jsoner.prettyPrint(json); System.out.println(json); // Java objects to JSON file try (FileWriter fileWriter = new FileWriter("C:\\projects\\user3.json")) { Jsoner.serialize(staff, fileWriter); private static Staff createStaff() { Staff staff = new Staff(); staff.setName("mkyong"); staff.setAge(38); staff.setPosition(new String[]{"Founder", "CTO", "Writer"}); Map<String, BigDecimal> salary = new HashMap() {{ put("2010", new BigDecimal(10000)); put("2012", new BigDecimal(12000)); put("2018", new BigDecimal(14000)); }}; staff.setSalary(salary); staff.setSkills(Arrays.asList("java", "python", "node", "kotlin")); return staff;
Output
Terminal
"skills":[ "java", "python", "node", "kotlin" ], "name":"mkyong", "position":[ "Founder", "CTO", "Writer" ], "salary":{ "2018":14000, "2012":12000, "2010":10000 }, "age":38
C:\\projects\\user3.json
{"skills":["java","python","node","kotlin"],"name":"mkyong","position":["Founder","CTO","Writer"],"salary":{"2018":14000,"2012":12000,"2010":10000},"age":38}
3.2 Convert list of objects to JSON array and save as .json file.
Staff staff = createStaff(); List<Staff> list = Arrays.asList(createStaff(), createStaff()); // list of Java objects to JSON file try (FileWriter fileWriter = new FileWriter("C:\\projects\\user4.json")) { Jsoner.serialize(list, fileWriter);
Output
C:\\projects\\user4.json
{"skills":["java","python","node","kotlin"],"name":"mkyong","position":["Founder","CTO","Writer"],"salary":{"2018":14000,"2012":12000,"2010":10000},"age":38}, {"skills":["java","python","node","kotlin"],"name":"mkyong","position":["Founder","CTO","Writer"],"salary":{"2018":14000,"2012":12000,"2010":10000},"age":38}
4. JSON to Java objects
4.1 To convert JSON back to Java objects, we need help from dozer third party library to copy the object properties.
pom.xml
<dependency> <groupId>net.sf.dozer</groupId> <artifactId>dozer</artifactId> <version>5.5.1</version> </dependency>
4.2 JSON string to Java object.
JsonSimple2.java
package com.mkyong; import com.github.cliftonlabs.json_simple.JsonException; import com.github.cliftonlabs.json_simple.JsonObject; import com.github.cliftonlabs.json_simple.Jsoner; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; import java.io.FileReader; import java.io.IOException; public class JsonSimple2 { public static void main(String[] args) throws IOException, JsonException { // The file `user3.json` is generated from above example 3.1 try (FileReader fileReader = new FileReader(("C:\\projects\\user3.json"))) { JsonObject deserialize = (JsonObject) Jsoner.deserialize(fileReader); // need dozer to copy object to staff, json_simple no api for this? Mapper mapper = new DozerBeanMapper(); // JSON to object Staff staff = mapper.map(deserialize, Staff.class); System.out.println(staff);
Output
Staff{name='mkyong', age=38, position=[Founder, CTO, Writer], skills=[java, python, node, kotlin], salary={2018=14000, 2012=12000, 2010=10000}}
4.3 JSON array to Java object.
JsonSimple3.java
package com.mkyong; import com.github.cliftonlabs.json_simple.JsonArray; import com.github.cliftonlabs.json_simple.JsonException; import com.github.cliftonlabs.json_simple.Jsoner; import org.dozer.DozerBeanMapper; import org.dozer.Mapper; import java.io.FileReader; import java.io.IOException; import java.util.List; import java.util.stream.Collectors; public class JsonSimple3 { public static void main(String[] args) throws IOException, JsonException { // The file `user4.json` is generated from above example 3.2 try (FileReader fileReader = new FileReader(("C:\\projects\\user4.json"))) { JsonArray objects = Jsoner.deserializeMany(fileReader); Mapper mapper = new DozerBeanMapper(); JsonArray o = (JsonArray) objects.get(0); List<Staff> collect = o.stream() .map(x -> mapper.map(x, Staff.class)).collect(Collectors.toList()); collect.forEach(x -> System.out.println(x));
Output
Staff{name='mkyong', age=38, position=[Founder, CTO, Writer], skills=[java, python, node, kotlin], salary={2018=14000, 2012=12000, 2010=10000}} Staff{name='mkyong', age=38, position=[Founder, CTO, Writer], skills=[java, python, node, kotlin], salary={2018=14000, 2012=12000, 2010=10000}}
From:一号门
Previous:Java Convert Array to ArrayList
COMMENTS