TutorialsArena

Working with JSON in Java using json-simple: Encoding and Decoding JSON Data

Learn how to use the json-simple library for efficient JSON processing in Java. This tutorial covers encoding Java objects to JSON and decoding JSON to Java objects, providing examples and best practices for handling JSON data in your Java applications.



Working with JSON in Java using json-simple

Using the json-simple Library

The json-simple library provides an easy way to work with JSON data in Java. It allows you to encode (convert Java objects to JSON) and decode (convert JSON to Java objects). The core classes are in the org.json.simple package: JSONObject, JSONArray, JSONValue, etc.

Setting up json-simple

To use json-simple, you need to include the json-simple.jar file in your project's classpath. Alternatively, if you're using Maven, add this dependency to your pom.xml:

Maven Dependency

<dependency>
  <groupId>com.googlecode.json-simple</groupId>
  <artifactId>json-simple</artifactId>
  <version>1.1</version>
</dependency>

JSON Encoding in Java

Here are examples of encoding JSON objects and arrays in Java:

Encoding a JSONObject

Code

import org.json.simple.JSONObject;

public class JsonExample1 {
    public static void main(String args[]) {
        JSONObject obj = new JSONObject();
        obj.put("name", "sonoo");
        obj.put("age", new Integer(27));
        obj.put("salary", new Double(600000));
        System.out.print(obj);
    }
}
Output

{"name":"sonoo","salary":600000.0,"age":27}

Encoding a JSONObject using a Map

Code

import org.json.simple.JSONValue;
import java.util.HashMap;
import java.util.Map;

public class JsonExample2 {
    public static void main(String args[]) {
        Map obj = new HashMap();
        obj.put("name", "sonoo");
        obj.put("age", new Integer(27));
        obj.put("salary", new Double(600000));
        String jsonText = JSONValue.toJSONString(obj);
        System.out.print(jsonText);
    }
}
Output

{"name":"sonoo","salary":600000.0,"age":27}

Encoding a JSONArray

Code

import org.json.simple.JSONArray;

public class JsonExample3 {
    public static void main(String args[]) {
        JSONArray arr = new JSONArray();
        arr.add("sonoo");
        arr.add(new Integer(27));
        arr.add(new Double(600000));
        System.out.print(arr);
    }
}
Output

["sonoo",27,600000.0]

Encoding a JSONArray using a List

Code

import org.json.simple.JSONValue;
import java.util.ArrayList;
import java.util.List;

public class JsonExample4 {
    public static void main(String args[]) {
        List arr = new ArrayList();
        arr.add("sonoo");
        arr.add(new Integer(27));
        arr.add(new Double(600000));
        String jsonText = JSONValue.toJSONString(arr);
        System.out.print(jsonText);
    }
}
Output

["sonoo",27,600000.0]

JSON Decoding in Java

Code

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

public class JsonDecodeExample1 {
    public static void main(String[] args) {
        String s = "{\"name\":\"sonoo\",\"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);
    }
}
Output

sonoo 600000.0 27