Question: What is JSON (JavaScript Object Notation)? A Data Representation Format (very similar to xml) Commonly used for APIs and Configs (widely used on the internet

What is JSON (JavaScript Object Notation)?

  • A Data Representation Format (very similar to xml)
  • Commonly used for APIs and Configs (widely used on the internet for APIs, config files for games, etc.)
  • Lightweight and Easy to Read/Write (small file size, easier to read then xml)
  • Integrates Easily with Most Languages

Environment

Before you start with encoding and decoding JSON using Java, you need to install any of the JSON modules available. For this lab, download JSON.simple fromhttps://code.google.com/archive/p/json-simple/downloads(Links to an external site.)and add the location of json-simple-1.1.1.jar file to the environment variable CLASSPATH.

For instance:

set CLASSPATH=.;C:\Program Files\Java\jdk-17.0.2\lib;C:\Program Files\Java\jdk-17.0.2\lib\mysql-connector-java-8.0.28.jar;C: :\Program Files\Java\jdk-17.0.2\lib\json-simple-1.1.1.jar

Mapping between JSON and Java entities

JSON.simple maps entities from the left side to the right side while decoding or parsing, and maps entities from the right to the left while encoding.

JSON

Java

string

java.lang.String

number

java.lang.Number

true|false

java.lang.Boolean

null

null

array

java.util.List

object

java.util.Map

On decoding, the default concrete class of java.util.List is org.json.simple.JSONArray and the default concrete class of java.util.Map is org.json.simple.JSONObject.

Try this simple encoding example and upload a snapshot using snipping tool (or photo):

JsonSimpleExample1.java:

import org.json.simple.JSONObject; import java.util.Arrays; import java.util.Collections; import java.util.Map; import java.util.HashMap; public class JsonSimpleExample1{ public static void main(String args[]){ Map jsonMap = new HashMap<>(); jsonMap.put("hello", "world!"); JSONObject obj = new JSONObject(jsonMap); System.out.print(obj); }}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!