Maven dependency -
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
|
If you are beginner don't worry learn how to Create new Maven project - In 2 minutes
Create JSON using json.simple.JSONObject in java
package com.ankit.json;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
//Create JSON using json.simple.JSONObject in java
public class CreateJson {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("emp_Name", "Ankit");
jsonObject.put("emp_id", 11);
JSONArray jsonArray = new JSONArray();
jsonArray.add("London");
jsonArray.add("Paris");
jsonObject.put("address", jsonArray);
System.out.print(jsonObject);
}
}
// Output
/*
{"address":["London","Paris"],"emp_Name":"Ankit","emp_id":11}
*/
|
Related links >
1. Jackson JSON -
We can use Jackson api for for processing JSON in java.
Jackson JSON examples
2. Java provides API (JSR 353) for Processing JSON (JSR 353).
It provides -
- Object Model API
- Streaming API
Java API for JSON processing examples >