- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert a Map to JSON object using JSON-lib API in Java?
A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format. Initially, we can create a POJO class and pass this instance as an argument to the put() method of Map class and finally add this map instance to the accumulateAll() method of JSONObject.
Syntax
public void accumulateAll(Map map)
In the below example, we can convert Map to a JSON object.
Example
import java.util.*; import net.sf.json.JSONObject; public class ConvertMapToJSONObjectTest { public static void main(String[] args)throws Exception { JSONObject jsonObject = new JSONObject(); Map<Integer, Employee> employees = new HashMap<Integer, Employee>(); employees.put(1, new Employee("Adithya", "Jai", 30)); employees.put(2, new Employee("Vamsi", "Krishna", 28)); employees.put(3, new Employee("Chaitanya", "Sai", 30)); jsonObject.accumulateAll(employees); System.out.println(jsonObject.toString(3)); // pretty print JSON } public static class Employee { private String firstName, lastName; private int age; public Employee(String firstName, String lastName, int age) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } } }
Output
{ "1": { "firstName": "Adithya", "lastName": "Jai", "age": 30 }, "2": { "firstName": "Vamsi", "lastName": "Krishna", "age": 28 }, "3": { "firstName": "Chaitanya", "lastName": "Sai", "age": 30 } }
- Related Articles
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert a JSON array to array using JSON-lib API in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to add elements to JSON Object using JSON-lib API in Java?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to convert an array to JSON Array using JSON-lib API in Java?
- How to convert a bean to XML using JSON-lib API in Java?
- How to convert a bean to XML without type hints using JSON-lib API in Java?
- How can we convert a map to the JSON object in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?
- Convert a Map to JSON using the Gson library in Java?
- Convert JSON to/from Map using Jackson library in Java?
- How can we convert a JSON string to a JSON object in Java?
- How to convert Java object to JSON using GSON library?
- How to convert Java object to JSON using Jackson library?

Advertisements