Using JSONObject in Java Application Tutorial


JSON(JavaScript Object Notation) is a text-based open standard designed for human-readable data interchange. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects. Despite its relationship to JavaScript, it is language-independent, with parsers available for many languages.

The JSON format is often used for serializing and transmitting structured data over a network connection. It is used primarily to transmit data between a server and web application, serving as an alternative to XML. It is lightweight and platform independent.

In this tutorial I am going to explain how to convert Java object to JSON and JSON to Java object using the simple JSON-lib(JSON-lib-x.x.x-jdk5.jar). JSON-lib is simple java library for transforming the beans, collections, maps, arrays and XML to JSON and then transforming them back to beans, collections, maps and others. The main class in this package is JSONObject.
The libraries required to working with JSON-lib are:
  • commons-lang.jar
  • commons-beanutils.jar
  • commons-collections.jar
  • commons-logging.jar
  • ezmorph.jar
  • json-lib-2.2.2-jdk15.jar
1. Converting Simple Object to JSON using JSONObject:

import net.sf.json.JSONObject;

public class JSONJavaObject {
    
    public static void main(String[] args){
        JSONObject jsonObject = new JSONObject();
        Employee employee = new Employee("123", "Kumar Swamy", "Software Engg", "kumar@codesuggestions.com");
        
        jsonObject.put("empId", employee.getEmpId());
        jsonObject.put("empName", employee.getEmpName());
        jsonObject.put("designation", employee.getDesingation());
        jsonObject.put("emailId", employee.getEmailId());
        
        System.out.println("Output: " + jsonObject);
    }
}

Output:

{"empId":"123","empName":"Kumar Swamy","designation":"Software Engg","emailId":"kumar@codesuggestions.com"}

2. Converting List of Java Objects to JSON using JSONObject:

import java.util.ArrayList;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JSONListObject {

    public static void main(String[] args) {
        List employees = new ArrayList();

        employees.add(new Employee("123", "Kumar Swamy", "Software Engg", "kumar@codesuggestions.com"));
        employees.add(new Employee("124", "Upender Godishala", "Team Lead", "upender@codesuggestions.com"));
        employees.add(new Employee("125", "Phani Rebba", "Project Lead", "phani@codesuggestions.com"));

        JSONObject jsonObject = getJsonFromEmployeeObject(employees);

        System.out.println(jsonObject);
    }

    public static JSONObject getJsonFromEmployeeObject(List employees) {
        JSONObject jsonResponse = new JSONObject();
        JSONArray jsonArray = new JSONArray();

        for (Employee employee : employees) {
            JSONObject jsonObject = new JSONObject();

            jsonObject.put("empId", employee.getEmpId());
            jsonObject.put("empName", employee.getEmpName());
            jsonObject.put("designation", employee.getDesingation());
            jsonObject.put("emailId", employee.getEmailId());

            jsonArray.add(jsonObject);
        }

        jsonResponse.put("employees", jsonArray);

        return jsonResponse;
    }
}

Output:

{"employees":[{"empId":"123","empName":"Kumar Swamy","designation":"Software Engg","emailId":"kumar@codesuggestions.com"},{"empId":"124","empName":"Upender Godishala","designation":"Team Lead","emailId":"upender@codesuggestions.com"},{"empId":"125","empName":"Phani Rebba","designation":"Project Lead","emailId":"phani@codesuggestions.com"}]}

3. Converting Map objects to JSON Object using JSONObject:

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

public class JSONMapObject {
    public static void main(String[] args){
        JSONObject jsonObject = new JSONObject();
        
        Map employees = new HashMap();
        
        employees.put(1, new Employee("123", "Kumar Swamy", "Software Engg", "kumar@codesuggestions.com"));
        employees.put(2, new Employee("124", "Upender Godishala", "Team Lead", "upender@codesuggestions.com"));
        employees.put(3, new Employee("125", "Phani Rebba", "Project Lead", "phani@codesuggestions.com"));
        
        jsonObject.accumulateAll(employees);
        
        System.out.println(jsonObject);
    }
}

Output:

{"1":{"desingation":"Software Engg","emailId":"kumar@codesuggestions.com","empId":"123","empName":"Kumar Swamy"},"2":{"desingation":"Team Lead","emailId":"upender@codesuggestions.com","empId":"124","empName":"Upender Godishala"},"3":{"desingation":"Project Lead","emailId":"phani@codesuggestions.com","empId":"125","empName":"Phani Rebba"}}

No comments

Powered by Blogger.