Вы находитесь на странице: 1из 6

2/25/2017 JSON Parsing using Java – Small Codes

(http://www.smlcodes.com/)

Tutorials (http://www.smlcodes.com/tutorials/) Java (http://www.smlcodes.com/category/java/)

Web Development (http://www.smlcodes.com/category/web-development/) Tools (http://www.smlcodes.com/category/tools/)

Examples (http://www.smlcodes.com/category/example/) Projects (http://www.smlcodes.com/category/projects/) More.. (http://www.smlcodes.com/category/more/)

← Online visual studio and Team foundation server Free (http://www.smlcodes.com/tools/servers/online-visual-studio-team-foundation-server-


free/)

XML Parsing in Java → (http://www.smlcodes.com/java/xml-parsing-java/)

JSON Parsing using Java


Posted by SmlCodes (http://www.smlcodes.com/author/satyakavetigmail-com/)

For dealing with JSON Parsing in java we have to use JSON.simple (https://code.google.com/archive/p/json-simple/). JSON.simple is a simple Java
toolkit for JSON. You can use JSON.simple to encode or decode JSON text.
 

Features
Full compliance with JSON speci䁾�cation (http://www.ietf.org/rfc/rfc4627.txt) (RFC4627) and reliable (see compliance testing)
Provides multiple functionalities such as encode, decode/parse and escape JSON text while keeping the library lightweight
Flexible, simple and easy to use by reusing Map and List interfaces
Supports streaming output of JSON text
Stoppable SAX-like interface for streaming input of JSON text
Heap based parser
High performance (see performance testing)
No dependency on external libraries
Both of the source code and the binary are JDK1.2 compatible
 

You can download from here (https://code.google.com/archive/p/json-simple/downloads), or use Maven Repository

1 <!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->


2 <dependency>
3     <groupId>com.googlecode.json-simple</groupId>
4     <artifactId>json-simple</artifactId>
5     <version>1.1</version>
6 </dependency>

1.Write JSON data to File Using Java


We want to save the JSON data as below.

JSON Data

http://www.smlcodes.com/java/json­parsing­using­java/ 1/6
2/25/2017 JSON Parsing using Java – Small Codes
1 {  
2    "website":"smlcodes.com",
3    "year":2016,
4    "articles":[  
5       "Java",
6       "JavaScript",
7       "Servers",
8       "Tools",
9       "DevOps"
10    ]
11 }

For doing so, we have to use below classes. Just know something about those classes.

JSONObject : it will store the data as <Key,Value>. It internally uses java.util.Map


JSONArray : for Storing Json Arrays we use this class object . it internally uses java.util.List

Steps to Write JSON data to 䁾�le


1.Create JSONObject object,put json values to this object

2.Create JSONArray Object, add array data to this object.


3.After that put JSONArray object into JSONObject.

Example : WriteJson.java Java


1 package json;
2 import java.io.FileWriter;
3 import java.io.IOException;
4 import org.json.simple.JSONArray;
5 import org.json.simple.JSONObject;
6  
7 public class WriteJson {
8     public static void main(String[] args) {
9         JSONObject obj = new JSONObject();
10         obj.put("website", "smlcodes.com");
11         obj.put("year", new Integer(2016));
12  
13         JSONArray array = new JSONArray();
14         array.add("Java");
15         array.add("JavaScript");
16         array.add("Servers");
17         array.add("Tools");
18         array.add("DevOps");
19  
20         obj.put("articles", array);
21  
22         try {
23  
24             FileWriter file = new FileWriter("D:\\Small Codes\\data.json");
25             // Make sure in path '\' is replaced by '\\'
26             file.write(obj.toString());
27             file.flush();
28             file.close();
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32         System.out.print(obj);
33     }
34 }

2.Read/Parse the JSON Data from JSON 䁾�le using Java


Last example we stored json data into data.json 䁾�le. Now we will see how to parse the json data from a 䁾�le.
For doing so, we need to know about below classes

JSONParser : we use parse(json䁾�le) method to parse the JSON 䁾�le. It will parse the JSON 䁾�le and arranges the data into <key,value> pairs
 

Steps:
1.Create JSONParser object, parse the json 䁾�le and store values into Object type.
2.Convert Object type values into JSONObject type values

http://www.smlcodes.com/java/json­parsing­using­java/ 2/6
2/25/2017 JSON Parsing using Java – Small Codes

3.use  get(“key”) method to get values

4.if get(“key”) returns JSONArray , use iterator() method to get array values
 

Example : ReadJson.java Java


1 package json;
2  
3 import java.io.FileReader;
4 import java.util.Iterator;
5 import org.json.simple.JSONArray;
6 import org.json.simple.JSONObject;
7 import org.json.simple.parser.JSONParser;
8  
9 public class ReadJson {
10     public static void main(String[] args) {
11  
12         try {
13             
14             //1.Create JSONParser object, parse the json file and store values into Object type
15             JSONParser parser = new JSONParser();
16             Object obj = parser.parse(new FileReader("D:\\Small Codes\\data.json"));
17             
18             
19             //2.Convert Object type values into JSONObject type values
20             JSONObject jsonObject = (JSONObject) obj;
21  
22             
23             //3.use  get(“key”) method to get values
24             String website = (String) jsonObject.get("website");
25             System.out.println("website :   "+website);
26             long yr = (Long) jsonObject.get("year");
27             System.out.println("Year    :   "+yr);
28  
29             // 4.use iterator() method to get array values
30             JSONArray array = (JSONArray) jsonObject.get("articles");
31             Iterator<String> iterator = array.iterator();
32             int i=0;
33             while (iterator.hasNext()) {
34                 i++;
35                 System.out.println("article["+i+"]    :  "+iterator.next());
36             }
37  
38         } catch (Exception e) {
39             e.printStackTrace();
40         }
41  
42     }
43 }

 Ref:
https://www.mkyong.com/java/json-simple-example-read-and-write-json/ (https://www.mkyong.com/java/json-simple-example-read-and-write-json/)
https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple/1.1.1 (https://mvnrepository.com/artifact/com.googlecode.json-
simple/json-simple/1.1.1)

http://central.maven.org/maven2/com/googlecode/json-simple/json-simple/ (http://central.maven.org/maven2/com/googlecode/json-simple/json-
simple/)
 

Share with Friend

 (http://www.smlcodes.com/java/json-parsing-using-java/?share=facebook&nb=1)
2
 (http://www.smlcodes.com/java/json-parsing-using-java/?share=tumblr&nb=1)

 (http://www.smlcodes.com/java/json-parsing-using-java/?share=email&nb=1)

 (whatsapp://send?text=JSON%20Parsing%20using%20Java http%3A%2F%2Fwww.smlcodes.com%2Fjava%2Fjson-parsing-using-java%2F)

 (http://www.smlcodes.com/java/json-parsing-using-java/#print)  (http://www.smlcodes.com/java/json-parsing-using-java/?share=linkedin&nb=1)
1

 (http://www.smlcodes.com/java/json-parsing-using-java/?share=twitter&nb=1)

Like this:

Loading...

http://www.smlcodes.com/java/json­parsing­using­java/ 3/6
2/25/2017 JSON Parsing using Java – Small Codes

Related

(http://www.smlcodes.com/tutorials/jackson- (http://www.smlcodes.com/java/web- (http://www.smlcodes.com/java/web-


json-tutorial/) services/22-jax-rs-restful-java-clients/) services/1-introduction-java-web-services/)
Jackson Json Tutorial 22. JAX-RS RESTFul Java Clients 1.Introduction to Java Web Services
(http://www.smlcodes.com/tutorials/jackson-json- (http://www.smlcodes.com/java/web-services/22-jax- (http://www.smlcodes.com/java/web-services/1-
tutorial/) rs-restful-java-clients/) introduction-java-web-services/)
January 27, 2017 January 12, 2017 January 7, 2017
In "All Posts" In "All Posts" In "All Posts"

 All Posts (http://www.smlcodes.com/category/all-posts/), Java (http://www.smlcodes.com/category/java/), JSON (http://www.smlcodes.com/category/web-development/json/), Tutorials


(http://www.smlcodes.com/category/tutorials/)
 JSON Parsing (http://www.smlcodes.com/tag/json-parsing/)

← Online visual studio and Team foundation server Free (http://www.smlcodes.com/tools/servers/online-visual-studio-team-foundation-server-


free/)

XML Parsing in Java → (http://www.smlcodes.com/java/xml-parsing-java/)

Leave a Reply
Enter your comment here...

Search …

Recent Posts
MongoDB Tutorial for Java Developers (http://www.smlcodes.com/tutorials/mongodb-tutorial-java-developers/)

MongoDB Tutorial (http://www.smlcodes.com/tutorials/mongodb-tutorial/)

JUnit Tutorial (http://www.smlcodes.com/tutorials/junit-tutorial/)

Apache Maven Tutorial (http://www.smlcodes.com/tutorials/apache-maven-tutorial/)

Apache Ant Tutorial (http://www.smlcodes.com/tutorials/apache-ant-tutorial/)

Archives
February 2017 (http://www.smlcodes.com/2017/02/)

January 2017 (http://www.smlcodes.com/2017/01/)

December 2016 (http://www.smlcodes.com/2016/12/)

November 2016 (http://www.smlcodes.com/2016/11/)

http://www.smlcodes.com/java/json­parsing­using­java/ 4/6
2/25/2017 JSON Parsing using Java – Small Codes

October 2016 (http://www.smlcodes.com/2016/10/)

September 2016 (http://www.smlcodes.com/2016/09/)

August 2016 (http://www.smlcodes.com/2016/08/)

July 2016 (http://www.smlcodes.com/2016/07/)

Categories
All Posts (http://www.smlcodes.com/category/all-posts/)

Books (http://www.smlcodes.com/category/more/books/)

Carrier Tips (http://www.smlcodes.com/category/more/carrier-tips/)

Core Java (http://www.smlcodes.com/category/java/core-java/)

Databases (http://www.smlcodes.com/category/tools/databases/)

DevOps (http://www.smlcodes.com/category/tools/devops/)

Examples (http://www.smlcodes.com/category/example/)

HTML (http://www.smlcodes.com/category/web-development/html/)

jar (http://www.smlcodes.com/category/more/jar/)

Java (http://www.smlcodes.com/category/java/)

Java Examples (http://www.smlcodes.com/category/example/java-examples/)

JDBC (http://www.smlcodes.com/category/java/jdbc/)

JSON (http://www.smlcodes.com/category/web-development/json/)

jsoup (http://www.smlcodes.com/category/java/jsoup/)

JSP (http://www.smlcodes.com/category/java/jsp/)

More.. (http://www.smlcodes.com/category/more/)

PHP (http://www.smlcodes.com/category/web-development/php/)

Servers (http://www.smlcodes.com/category/tools/servers/)

Servlets (http://www.smlcodes.com/category/java/servlets/)

Struts (http://www.smlcodes.com/category/java/struts/)

Tools (http://www.smlcodes.com/category/tools/)

Tutorials (http://www.smlcodes.com/category/tutorials/)

Web Development (http://www.smlcodes.com/category/web-development/)

Web Services (http://www.smlcodes.com/category/java/web-services/)

Web Services Examples (http://www.smlcodes.com/category/example/web-services-examples/)

http://www.smlcodes.com/java/json­parsing­using­java/ 5/6
2/25/2017 JSON Parsing using Java – Small Codes

wordpress (http://www.smlcodes.com/category/web-development/wordpress/)

XML (http://www.smlcodes.com/category/web-development/xml/)

Pages

About (http://www.smlcodes.com/about/)

Ameerpet Materials (http://www.smlcodes.com/ameerpetmaterials/)

Compiler (http://www.smlcodes.com/compiler/)

Contact US (http://www.smlcodes.com/contact/)

Download Projects (http://www.smlcodes.com/projects/)

Example Programs (http://www.smlcodes.com/programs/)

Posts (http://www.smlcodes.com/posts/)

Privacy Policy (http://www.smlcodes.com/privacy/)

SmlCodes – Programming Simpli䁾�ed (http://www.smlcodes.com/)

Terms and Conditions (http://www.smlcodes.com/terms/)

Tutorials (http://www.smlcodes.com/tutorials/)

Search …

Archives

February 2017 (http://www.smlcodes.com/2017/02/)

January 2017 (http://www.smlcodes.com/2017/01/)

December 2016 (http://www.smlcodes.com/2016/12/)

November 2016 (http://www.smlcodes.com/2016/11/)

October 2016 (http://www.smlcodes.com/2016/10/)

September 2016 (http://www.smlcodes.com/2016/09/)

August 2016 (http://www.smlcodes.com/2016/08/)

July 2016 (http://www.smlcodes.com/2016/07/)

Meta

Log in (http://www.smlcodes.com/wp-login.php)

http://www.smlcodes.com/java/json­parsing­using­java/ 6/6

Вам также может понравиться