What is JSON (JavaScript Object Notation)? A Data Representation Format (very similar to xml) Commonly used for
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 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
Accounting concepts and applications
ISBN: 978-0538745482
11th Edition
Authors: Albrecht Stice, Stice Swain