Launch your tech mastery with us—your coding journey starts now!
Course Content
Advanced Java

JSON with Java

This chapter covers how to encode and decode JSON objects using Java programming language. Let’s start with preparing the environment to start our programming with Java for JSON.

Environment

Before you start with encoding and decoding JSON using Java, you need to install any of the JSON modules available. For this tutorial we have downloaded and installed JSON.simple and have added the location of json-simple-1.1.1.jar file to the environment variable CLASSPATH.

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 isorg.json.simple.JSONArray and the default concrete class of java.util.Map isorg.json.simple.JSONObject.

Encoding JSON in Java

Following is a simple example to encode a JSON object using Java JSONObject which is a subclass of java.util.HashMap. No ordering is provided. If you need the strict ordering of elements, use JSONValue.toJSONString ( map ) method with ordered map implementation such as java.util.LinkedHashMap.

 

import org.json.simple.JSONObject;

class JsonEncodeDemo {

   public static void main(String[] args){

      JSONObject obj = new JSONObject();

      obj.put(“name”, “foo”);

      obj.put(“num”, new Integer(100));

      obj.put(“balance”, new Double(1000.21));

      obj.put(“is_vip”, new Boolean(true));

      System.out.print(obj);

   }

}

On compiling and executing the above program the following result will be generated −

{“balance”: 1000.21, “num”:100, “is_vip”:true, “name”:”foo”}

Following is another example that shows a JSON object streaming using Java JSONObject −

import org.json.simple.JSONObject;

 

class JsonEncodeDemo {

   public static void main(String[] args){

      JSONObject obj = new JSONObject();

      obj.put(“name”,”foo”);

      obj.put(“num”,new Integer(100));

      obj.put(“balance”,new Double(1000.21));

      obj.put(“is_vip”,new Boolean(true));

      StringWriter out = new StringWriter();

      obj.writeJSONString(out);

     

      String jsonText = out.toString();

      System.out.print(jsonText);

   }

}

On compiing and executing the above program, the following result is generated −

{“balance”: 1000.21, “num”:100, “is_vip”:true, “name”:”foo”}

Decoding JSON in Java

The following example makes use of JSONObject 

import org.json.simple.JSONObject;  

import org.json.simple.JSONValue;  

public class JsonDecode {  

public static void main(String[] args) {  

    String s=”{“name”:”sycs”,”salary”:600000.0,”age”:27}”;  

    Object obj=JSONValue.parse(s);  

    JSONObject jsonObject = (JSONObject) obj;  

    String name = (String) jsonObject.get(“name”);  

    double salary = (Double) jsonObject.get(“salary”);  

    long age = (Long) jsonObject.get(“age”);  

    System.out.println(name+” “+salary+” “+age);  

}