![]() |
VOOZH | about |
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
<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
Let us see the ways how to create a JSON object
JsonEncodeExample.java
On running the above program we can see the output as
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
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)
Let us see how to read the JSON via
JSOnReadExample.java
On running the program, we can see the output as follows:
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.