VOOZH about

URL: https://www.geeksforgeeks.org/java/what-is-json-java-org-json/

⇱ What is JSON-Java (org.json)? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

What is JSON-Java (org.json)?

Last Updated : 17 Jul, 2022

JSON(Javascript Object Notation) is a lightweight format of data exchange and it is independent of language. It is easily read and interpreted by humans and machines. Hence nowadays, everywhere JSON is getting used for transmitting data. Let us see how to prepare JSON data by using JSON.org JSON API  in java

  • JSON.simple is a library in java is used that allows parsing/generating/transforming/querying  JavaScript Object Notation, etc.,
  • Necessary jar file:  json-simple-1.1.jar. It has to be available in the classpath
  • In Maven-driven projects it should be present in pom.xml as follows
<dependency>
 <groupId>com.googlecode.json-simple</groupId>
 <artifactId>json-simple</artifactId>
 <version>1.1.1</version>
</dependency>

We can store the items in the form of JSONObject or JSONArray. JSONArray is nothing but the collection of multiple JSON Objects. A sample JSON object can be viewed as follows

👁 Image
 

Let us see the ways how to create a JSON object  

JsonEncodeExample.java

On running the above program we can see the output as 

👁 Image
 

If we beautify this using the online JSON viewer, we will get the same output as appeared in the sample JSON

JsonDecodeExample.java

On running the above program, we get the output as follows

👁 Image
 

Let us see how the JSONObject and JSONArray can be written in an external file named "author.json" via

JSONWriteExample.java

On running the above program, we can see the "author.json" file is created in the mentioned location and its contents are as follows  (The output is beautified and shown here as reference)

👁 Image
 

Let us see how to read the JSON via

JSOnReadExample.java

On running the program, we can see the output as follows:

👁 output
 

Conclusion

JSON is a lightweight easily readable, understandable, and iteratable transportation medium. Nowadays it is the output mechanism that got followed in any REST API calls. This way of mechanism is easily understood in android and IOS development and hence one JSON output of REST API call serves the purpose of many communications.

Comment
Article Tags:
Article Tags: