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 [Link]
and have added the location of [Link] file to the environment variable
CLASSPATH.
Mapping between JSON and Java entities
[Link] 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 [Link]
number [Link]
true|false [Link]
null null
array [Link]
object [Link]
On decoding, the default concrete class of [Link] is [Link] and the
default concrete class of [Link] is [Link].
Encoding JSON in Java
Following is a simple example to encode a JSON object using Java JSONObject which is a
subclass of [Link]. No ordering is provided. If you need the strict ordering of
elements, use [Link] ( map ) method with ordered map implementation
such as [Link].
import [Link];
class JsonEncodeDemo {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
[Link]("name", "foo");
[Link]("num", new Integer(100));
[Link]("balance", new Double(1000.21));
[Link]("is_vip", new Boolean(true));
[Link](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 [Link];
class JsonEncodeDemo {
public static void main(String[] args) {
JSONObject obj = new JSONObject();
[Link]("name","foo");
[Link]("num",new Integer(100));
[Link]("balance",new Double(1000.21));
[Link]("is_vip",new Boolean(true));
StringWriter out = new StringWriter();
[Link](out);
String jsonText = [Link]();
[Link](jsonText);
}
}
On compiling 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 and JSONArray where JSONObject is a
[Link] and JSONArray is a [Link], so you can access them with standard
operations of Map or List.
import [Link];
import [Link];
import [Link];
import [Link];
class JsonDecodeDemo {
public static void main(String[] args) {
JSONParser parser = new JSONParser();
String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
try{
Object obj = [Link](s);
JSONArray array = (JSONArray)obj;
[Link]("The 2nd element of array");
[Link]([Link](1));
[Link]();
JSONObject obj2 = (JSONObject)[Link](1);
[Link]("Field \"1\"");
[Link]([Link]("1"));
s = "{}";
obj = [Link](s);
[Link](obj);
s = "[5,]";
obj = [Link](s);
[Link](obj);
s = "[5,,2]";
obj = [Link](s);
[Link](obj);
}catch(ParseException pe) {
[Link]("position: " + [Link]());
[Link](pe);
}
}
}
On compiling and executing the above program, the following result will be generated −
The 2nd element of array
{"1":{"2":{"3":{"4":[5,{"6":7}]}}}}
Field "1"
{"2":{"3":{"4":[5,{"6":7}]}}}
{}
[5]
[5,2]