Maven dependency -
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.1</version>
</dependency>
|
If you are beginner don't worry learn how to Create new Maven project - In 2 minutes
Create Json using Streaming Model API - using
package com.ankit.json;
import java.io.StringWriter;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
public class CreateJsonUsingStreamingModelAPI {
public static void main(String[] args) {
StringWriter StringWriter = new StringWriter();
JsonGenerator jsonGenerator = Json.createGenerator(StringWriter);
jsonGenerator = jsonGenerator.writeStartObject();
jsonGenerator.write("emp_Name", "Ankit");
jsonGenerator.write("emp_id", 11);
jsonGenerator.writeEnd();
jsonGenerator.close();
System.out.println(StringWriter.toString());
}
}
//Output
/*
{"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 >