
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to wrap a JSON using flexjson in Java?
The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.
Syntax
public JSONSerializer rootName(String rootName)
Example
import flexjson.JSONSerializer; public class JSONRootNameTest { public static void main(String[] args) { JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true); Employee emp = new Employee("Adithya", "Jai", 28, "Hyderabad"); String jsonStr = serializer.serialize(emp); System.out.println(jsonStr); } } // Employee class class Employee { private String firstName; private String lastName; private int age; private String address; public Employee() {} public Employee(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public int getAge() { return age; } public String getAddress() { return address; } }
Output
{ "My_Employee": { "address": "Hyderabad", "age": 28, "class": "Employee", "firstName": "Adithya", "lastName": "Jai" } }
- Related Articles
- How to deserialize a JSON to Java object using the flexjson in Java?
- How to control serialization through @JSON annotation using flexjson in Java?\n
- Pretty print JSON using the flexjson library in Java?\n
- How to deserialize a Java object from Reader Stream using flexjson in Java?
- How to serialize a map using the flexjson library in Java?\n
- How can we serialize a list of objects using flexjson in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert a Map 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 can we serialize an array of objects using flexjson in Java?
- 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?\n
- How to add elements to JSON Object using JSON-lib API in Java?
- How to Write/create a JSON file using Java?
- How to Write/create a JSON array using Java?

Advertisements