Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Accounting concepts and applications

Authors: Albrecht Stice, Stice Swain

11th Edition

978-0538750196, 538745487, 538750197, 978-0538745482

More Books

Students also viewed these Programming questions