JSON Simple example show you how to use JSON.simple Java library for reading and writing JSON data into a file.
JSON.simple, is a simple Java library for JSON processing and full compliance with JSON specification (RFC4627).
Other interesting posts you may like
In this tutorial, we show you how to use JSON.simple to read and write JSON data from / to a file.
Project structure directory
In this JSON Simple example, we will create java project with directory structure like below:
Maven dependencies
We should provide JSON.simple Dependency in pom.xml file like below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <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.javabycode.json</groupId> <artifactId>json-simple</artifactId> <version>1.0.0-SNAPSHOT</version> <name>JSON-EXAMPLE - ${project.artifactId}</name> <packaging>jar</packaging> <dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> </dependencies> </project> |
Create a program that write JSON to file
In this example, it initializes data via JSONObject and JSONArray. Then save it into a file named “test.json” that is placed in the directory “E:\tmp”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
package com.javabycode.json; import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class WriteJsonToFileExample { @SuppressWarnings("unchecked") public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "javabycode.com"); obj.put("visitor", new Integer(100)); JSONArray array = new JSONArray(); array.add("Learn Spring by Examples"); array.add("Learn Hibernate by Examples"); array.add("Learn Java by Examples"); obj.put("titles", array); try { FileWriter file = new FileWriter("E:\\tmp\\test.json"); file.write(obj.toJSONString()); file.flush(); file.close(); } catch (IOException e) { e.printStackTrace(); } System.out.print(obj); } } |
Run the above program as java application and get the output in the file “test.json” like below:
1 |
{"name":"javabycode.com","titles":["Learn Spring by Examples","Learn Hibernate by Examples","Learn Java by Examples"],"visitor":100} |
Create a program that read JSON from file
In this program, we use JSONParser to read above the generated JSON file “test.json” and display it’s content
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.javabycode.json; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Iterator; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class ReadJsonFromFileExample { @SuppressWarnings("unchecked") public static void main(String[] args) { JSONParser parser = new JSONParser(); try { Object obj = parser.parse(new FileReader("E:\\tmp\\test.json")); JSONObject jsonObject = (JSONObject) obj; String name = (String) jsonObject.get("name"); System.out.println(name); long age = (Long) jsonObject.get("visitor"); System.out.println(age); // loop array JSONArray msg = (JSONArray) jsonObject.get("titles"); Iterator<String> iterator = msg.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } } } |
Run above program as java application and output will be generated like below:
1 2 3 4 5 |
javabycode.com 100 Learn Spring by Examples Learn Hibernate by Examples Learn Java by Examples |
Noticed that if you want to convert object to / from JSON, you should consider Jackson or Gson.
That’t it on the JSON Simple example tutorial.
Download source code, click link below
jsonsimple-example.zip
There’s definately a great deal to find out about this topic.
I love all of the points you made.
Hi, I do believe this is an excellent web site.
I stumbledupon it 😉 I am going to come back once again since i
have book marked it.
Greate pieces. Keep posting such kind of information on your site.
Im really impressed by it.
Hello there, You have performed an excellent job. I
will certainly digg it and individually suggest to my friends.
I am sure they will be benefited from this site.