Read and write JSON using JSON.simple
This article shows how to read and write JSON using JSON.simple.
Table of contents:
- 1. Setup JSON.simple
- 2. Write JSON to File using JSON.simple
- 3. Read JSON from File using JSON.simple
- 4. Java object to JSON using JSON.simple
- 5. JSON to Java Object using JSON.simple
- 6. Download Source Code
- 7. References
P.S Tested with json-simple 4.0.1
JSON.simple short history
This project was formerly JSON.simple 1.x from a Google code project by Yidong, now maintained by Clifton Labs, read this JSON.simple history
1. Setup JSON.simple
Declares json-simple
in the pom.xml
file.
<dependency>
<groupId>com.github.cliftonlabs</groupId>
<artifactId>json-simple</artifactId>
<version>4.0.1</version>
</dependency>
2. Write JSON to File using JSON.simple
The following example uses JSON.simple JsonObject
and JsonArray
to create JSON and write it to a file named preson.json
.
package com.mkyong.json.jsonsimple;
import com.github.cliftonlabs.json_simple.JsonArray;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;
import java.io.FileWriter;
import java.io.IOException;
public class JsonSimpleWriteExample1 {
public static void main(String[] args) {
// JSON String
JsonObject jsonObject = new JsonObject();
jsonObject.put("name", "mkyong");
jsonObject.put("age", 42);
// JSON Array
JsonArray list = new JsonArray();
list.add("msg 1");
list.add("msg 2");
list.add("msg 3");
jsonObject.put("messages", list);
try (FileWriter fileWriter = new FileWriter("person.json")) {
Jsoner.serialize(jsonObject, fileWriter);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Output
{"name":"mkyong","messages":["msg 1","msg 2","msg 3"],"age":42}
3. Read JSON from File using JSON.simple
The following example uses JSON.simple to read JSON from a file named person.json
and print out the values.
{
"name": "mkyong",
"messages": [
"msg 1",
"msg 2",
"msg 3"
],
"age": 42
}
package com.mkyong.json.jsonsimple;
import com.github.cliftonlabs.json_simple.JsonArray;
import com.github.cliftonlabs.json_simple.JsonException;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;
import java.io.FileReader;
import java.io.IOException;
public class JsonSimpleReadExample1 {
public static void main(String[] args) {
try (FileReader reader = new FileReader("person.json")) {
JsonObject jsonObject = (JsonObject) Jsoner.deserialize(reader);
// read value one by one manually
System.out.println((String) jsonObject.get("name"));
System.out.println(((Number) jsonObject.get("age")).intValue());
// loops the array
JsonArray msg = (JsonArray) jsonObject.get("messages");
for (Object o : msg) {
System.out.println(o);
}
} catch (IOException | JsonException e) {
throw new RuntimeException(e);
}
}
}
Output
mkyong
42
msg 1
msg 2
msg 3
4. Java object to JSON using JSON.simple
In JSON.simple, to convert a Java object to JSON, the Java object needs to implement the Jsonable
interface and override the toJson()
methods to tell how the mapping should work.
package com.mkyong.json.jsonsimple.model;
import com.github.cliftonlabs.json_simple.JsonArray;
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.util.List;
public class Person implements Jsonable {
private String name;
private int age;
private List<String> messages;
public Person() {
}
public Person(String name, int age, List<String> messages) {
this.name = name;
this.age = age;
this.messages = messages;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getMessages() {
return messages;
}
public void setMessages(List<String> messages) {
this.messages = messages;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", messages=" + messages +
'}';
}
@Override
public String toJson() {
final StringWriter writable = new StringWriter();
try {
this.toJson(writable);
} catch (final IOException caught) {
throw new RuntimeException(caught);
}
return writable.toString();
}
@Override
public void toJson(Writer writer) throws IOException {
JsonObject json = new JsonObject();
json.put("name", this.name);
json.put("age", this.age);
json.put("messages", new JsonArray(this.messages));
json.toJson(writer);
}
}
The following example uses JSON.simple to convert a Java object named Person
into a JSON-formatted string and write it to a file named person.json
.
package com.mkyong.json.jsonsimple;
import com.github.cliftonlabs.json_simple.Jsoner;
import com.mkyong.json.jsonsimple.model.Person;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JsonSimpleWriteExample2 {
public static void main(String[] args) {
// this object need implements Jsonable interface
Person person = new Person();
person.setName("mkyong");
person.setAge(42);
List<String> list = new ArrayList<>();
list.add("msg 1");
list.add("msg 2");
list.add("msg 3");
person.setMessages(list);
try (FileWriter fileWriter = new FileWriter("person.json")) {
// convert object to json and write to file
Jsoner.serialize(person, fileWriter);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Output
{"name":"mkyong","messages":["msg 1","msg 2","msg 3"],"age":42}
5. JSON to Java Object using JSON.simple
The following example uses JSON.simple to read JSON from a file named person.json
, and convert the JSON back to the Java object Person
.
{
"name": "mkyong",
"messages": [
"msg 1",
"msg 2",
"msg 3"
],
"age": 42
}
package com.mkyong.json.jsonsimple;
import com.github.cliftonlabs.json_simple.JsonArray;
import com.github.cliftonlabs.json_simple.JsonException;
import com.github.cliftonlabs.json_simple.JsonObject;
import com.github.cliftonlabs.json_simple.Jsoner;
import com.mkyong.json.jsonsimple.model.Person;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JsonSimpleReadExample2 {
public static void main(String[] args) {
try (FileReader reader = new FileReader("person.json")) {
JsonObject jsonObject = (JsonObject) Jsoner.deserialize(reader);
// convert object to json, need manual, no ready api.
// the simple of JSON.simple.
Person person = new Person();
person.setName((String) jsonObject.get("name"));
person.setAge(((Number) jsonObject.get("age")).intValue());
JsonArray jsonArray = (JsonArray) jsonObject.get("messages");
List<String> messages = new ArrayList<>();
for (Object obj : jsonArray) {
messages.add((String) obj);
}
person.setMessages(messages);
System.out.println(person);
} catch (IOException | JsonException e) {
throw new RuntimeException(e);
}
}
}
Output
Person{name='mkyong', age=42, messages=[msg 1, msg 2, msg 3]}
6. Download Source Code
$ git clone https://github.com/mkyong/java-json
$ cd simplejson
jjjjjjjjj