Easy Tutorial
❮ Html5 Canvas Clock Android Tutorial Absolutelayout ❯

Use of JSON in Java

Category Programming Techniques

In this section, we will introduce how to use JSON in the Java language.

Library Selection

Java does not have built-in JSON parsing, so using JSON requires the use of third-party libraries.

Here are several commonly used JSON parsing libraries:

The following tutorial is based on FastJson.

Environment Configuration

In a Maven-built project, add the following dependency to the pom.xml file.

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.47</version>
</dependency>

Encoding and Decoding

Encoding

The encoding process from Java variables to JSON format is as follows:

Example

public void testJson() {
    JSONObject object = new JSONObject();
    //string
    object.put("string","string");
    //int
    object.put("int",2);
    //boolean
    object.put("boolean",true);
    //array
    List<Integer> integers = Arrays.asList(1,2,3);
    object.put("list",integers);
    //null
    object.put("null",null);
​
    System.out.println(object);
}

In the above example, a JSON object is first created, then strings, integers, boolean values, and arrays are added in sequence, and finally, it is printed as a string.

The output result is as follows:

{"boolean":true,"string":"string","list":[1,2,3],"int":2}

Decoding

The decoding process from JSON objects to Java variables is as follows:

Example

public void testJson2() {
​
  JSONObject object = JSONObject
      .parseObject("{\"boolean\":true,\"string\":\"string\",\"list":[1,2,3],\"int\":2}");
  //string
  String s = object.getString("string");
  System.out.println(s);
  //int
  int i = object.getIntValue("int");
  System.out.println(i);
  //boolean
  boolean b = object.getBooleanValue("boolean");
  System.out.println(b);
  //list
  List<Integer> integers = JSON.parseArray(object.getJSONArray("list").toJSONString(),Integer.class);
  integers.forEach(System.out::println);
  //null
  System.out.println(object.getString("null"));
​
}

In the above example, a JSON object is first constructed from a JSON-formatted string, then strings, integers, boolean values, and arrays are read in sequence, and finally, they are printed one by one, with the printing results as follows:

string
2
true
1
2
3
null

Conversion between JSON Objects and Strings

Method Purpose
JSON.parseObject() Parse JSON object from string
JSON.parseArray() Parse JSON array from string
JSON.toJSONString(obj/array) Convert JSON object or JSON array to string

Example

//Parse JSON object from string
JSONObject obj = JSON.parseObject("{\"tutorialpro\":\"tutorialpro.org\"}");
//Parse JSON array from string
JSONArray arr = JSON.parseArray("[\"tutorialpro.org\",\"tutorialpro\"]\n");
//Convert JSON object to string
String objStr = JSON.toJSONString(obj);
//Convert JSON array to string
String arrStr = JSON.toJSONString(arr);

Related Tutorials

**Share My Notes

Cancel

-

-

-

❮ Html5 Canvas Clock Android Tutorial Absolutelayout ❯