
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Differences Between org.simple.json and org.json Libraries in Java
In Java, org.simple.json and org.json are two libraries that help in reading, writing, and manipulating JSON. But still, they are different. In this article, we are going to learn about these differences.
Difference between JSON.simple vs JSON
Let's see the below differences and they are -
Features | JSON.simple | JSON |
---|---|---|
Purpose | Read and write JSON data in Java (encode and decode JSON objects). | Parse JSON. |
Conversion | JSON operations only. | Converts between JSON and XML, HTTP headers, Cookies, and CDF. |
Key Classes | JSONValue, JSONObject, JSONArray, JsonString, JsonNumber. | JSONObject, JSONTokener, JSONWriter, JSONArray, CDL, Cookie, CookieList. |
Required Jar | json.simple.jar to execute the JSON program file. | json.jar to execute the JSON program file. |
Example for org.simple.json package
In the given program, it creates a JSONObject, adds key-value pairs, and gives output, JSON as a string using the toJSONString() method.
import org.json.simple.JSONObject; public class SimpleJsonTest { public static void main(String[] args) { JSONObject jsonObj = new JSONObject(); jsonObj.put("empName", "Raja"); jsonObj.put("employeeId", "115"); jsonObj.put("age","30"); System.out.println(jsonObj.toJSONString()); } }
Following is the output of the above program -
{"empName":"Raja","employeeId":"115","age":"30"}
Example for org.json package
Following example prints the contents of a JSON document using the org.json library -
import org.json.*; public class JSONTest { public static void main(String args[]) throws JSONException { String json = "{" + ""Name": "Jai"," + ""Age": 25, " + ""Salary": 25000.00 " + "}"; JSONObject jsonObj = new JSONObject(json); System.out.println(jsonObj.toString()); } }
Following is the output of the above program -
{"Name":"Jai","Age":25,"Salary":25000}
Advertisements