Maven Dependency
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.8</version>
</dependency>
|
How to create JSON message in Java?
public static void createJSONMessage() throws JSONException {
JSONObject jsonObject = new JSONObject();
//Write String value
jsonObject.put("FirstName", "Java");
jsonObject.put("LastName", "Guider");
//Write Double value
jsonObject.put("Version", 1.8);
//Write boolean value
jsonObject.put("PlatformIndependent", true);
//Create JSONArray Object
JSONArray jsonArray = new JSONArray();
jsonArray.put(0, "London");
jsonArray.put(1, "United Kingdom");
//Write Object value
jsonObject.put("Address", jsonArray);
System.out.println("createJSONMessage :: JSON Object as String :: " + jsonObject.toString());
}
|
How to read JSON message in Java?
public static void readJSONMessage(String input) throws JSONException {
//Convert Input Message to JSONObject
JSONObject jsonObject = new JSONObject(input);
//Print all elements from JSONObject
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
System.out.println("readJSONMessage :: Key ::" + key + ", Value:: " + jsonObject.get(key));
}
}
|
How to validate JSON message in Java?
public static boolean isJsonValid(String input){
boolean result = false;
try {
JSONObject jsonObject = new JSONObject(input);
result = true;
System.out.println("isJsonValid :: Input Message [" + input + "] is a VALID JSON Format.");
} catch (JSONException e) {
System.out.println("isJsonValid :: Input Message [" + input + "] is a NOT VALID JSON Format.");
e.printStackTrace();
}
return result;
}
|
How to check null key in JSON message?
public static void doesJsonInputContainNullValue(String input) throws JSONException {
// Convert Input Message to JSONObject
JSONObject jsonObject = new JSONObject(input);
// Iterate all elements from JSONObject
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
// check Null JSON value
if (jsonObject.isNull(key)) {
System.out.println("doesJsonInputContainNullValue :: JSON key which stores Null value :: " + key );
}
}
}
|
How to convert JSON message into Map in Java?
public static Map<String, Object> convertJSONStringToMap(String input) throws JSONException {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
// Convert Input Message to JSONObject
JSONObject jsonObject = new JSONObject(input);
// Iterate all elements from JSONObject
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
// Add all elements in HashMap
hashMap.put(key, jsonObject.get(key));
}
System.out.println("convertJSONStringToMap :: JSON in HashMap ::" + hashMap);
return hashMap;
}
|
Source Code Of JSON Operations
package com.kt4study.json;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* @author kt4study.blogspot.co.uk
*
*/
public class JettisonJSONExample {
public static void createJSONMessage() throws JSONException {
JSONObject jsonObject = new JSONObject();
//Write String value
jsonObject.put("FirstName", "Java");
jsonObject.put("LastName", "Guider");
//Write Double value
jsonObject.put("Version", 1.8);
//Write boolean value
jsonObject.put("PlatformIndependent", true);
//Create JSONArray Object
JSONArray jsonArray = new JSONArray();
jsonArray.put(0, "London");
jsonArray.put(1, "United Kingdom");
//Write Object value
jsonObject.put("Address", jsonArray);
System.out.println("createJSONMessage :: JSON Object as String :: " + jsonObject.toString());
}
public static void readJSONMessage(String input) throws JSONException {
//Convert Input Message to JSONObject
JSONObject jsonObject = new JSONObject(input);
//Print all elements from JSONObject
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
System.out.println("readJSONMessage :: Key ::" + key + ", Value:: " + jsonObject.get(key));
}
}
public static boolean isJsonValid(String input){
boolean result = false;
try {
JSONObject jsonObject = new JSONObject(input);
result = true;
System.out.println("isJsonValid :: Input Message [" + input + "] is a VALID JSON Format.");
} catch (JSONException e) {
System.out.println("isJsonValid :: Input Message [" + input + "] is a NOT VALID JSON Format.");
e.printStackTrace();
}
return result;
}
// Print All JSON keys which stores Null value
public static void doesJsonInputContainNullValue(String input) throws JSONException {
// Convert Input Message to JSONObject
JSONObject jsonObject = new JSONObject(input);
// Iterate all elements from JSONObject
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
// check Null JSON value
if (jsonObject.isNull(key)) {
System.out.println("doesJsonInputContainNullValue :: JSON key which stores Null value :: " + key );
}
}
}
public static Map<String, Object> convertJSONStringToMap(String input) throws JSONException {
HashMap<String, Object> hashMap = new HashMap<String, Object>();
// Convert Input Message to JSONObject
JSONObject jsonObject = new JSONObject(input);
// Iterate all elements from JSONObject
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
// Add all elements in HashMap
hashMap.put(key, jsonObject.get(key));
}
System.out.println("convertJSONStringToMap :: JSON in HashMap ::" + hashMap);
return hashMap;
}
//Convert JSON String into Map
public static void main(String[] args) throws JSONException {
JettisonJSONExample.createJSONMessage();
JettisonJSONExample.readJSONMessage("{\"FirstName\":\"Java\",\"LastName\":\"Guider\",\"OS\":null,\"Version\":1.8,\"PlatformIndependent\":true,\"Address\":[\"London\",\"United Kingdom\"]}");
JettisonJSONExample.isJsonValid("{\"FirstName\":\"Java Guider\"}");
JettisonJSONExample.doesJsonInputContainNullValue("{\"FirstName\":\"Java\",\"LastName\":\"Guider\",\"OS\":null}");
JettisonJSONExample.convertJSONStringToMap("{\"FirstName\":\"Java\",\"LastName\":\"Guider\",\"OS\":null,\"Version\":1.8,\"PlatformIndependent\":true,\"Address\":[\"London\",\"United Kingdom\"]}");
}
}
|
Output →
createJSONMessage :: JSON Object as String :: {"FirstName":"Java","LastName":"Guider","Version":1.8,"PlatformIndependent":true,"Address":["London","United Kingdom"]}
readJSONMessage :: Key ::FirstName, Value:: Java
readJSONMessage :: Key ::LastName, Value:: Guider
readJSONMessage :: Key ::OS, Value:: null
readJSONMessage :: Key ::Version, Value:: 1.8
readJSONMessage :: Key ::PlatformIndependent, Value:: true
readJSONMessage :: Key ::Address, Value:: ["London","United Kingdom"]
isJsonValid :: Input Message [{"FirstName":"Java Guider"}] is a VALID JSON Format.
doesJsonInputContainNullValue :: JSON key which stores Null value :: OS
convertJSONStringToMap :: JSON in HashMap ::{OS=null, Address=["London","United Kingdom"], Version=1.8, FirstName=Java, LastName=Guider, PlatformIndependent=true}