
- 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 can we encode a JSON object in Java?
A JSONObject is a subclass of java.util.HashMap where no order is provided. We can also use the strict ordering of elements as well with the help of the JSONValue.toJSONString(map) method i.e. by the implementation of java.util.LinkedHashMap.
We can encode a JSON object in the below two examples.
Example
import java.util.*; import org.json.simple.JSONObject; public class JSONEncodingTest { public static void main(String[] args) { Map<Object, Object> dataMap = new HashMap<Object, Object>(); dataMap.put("Name", "Adithya"); dataMap.put("Age", new Integer(25)); dataMap.put("Salary", new Double(25000.00)); dataMap.put("Employee Id", new Integer(115)); dataMap.put("Company", "TutorialsPoint"); JSONObject jsonObj = new JSONObject(dataMap); System.out.print("Encoding a JSON object: "); System.out.print(jsonObj); } }
Output
Encoding a JSON object: {"Salary":25000.0,"Employee id":115,"Company":"TutorialsPoint","Age":25,"Name":"Adithya"}
Example
import java.io.*; import org.json.simple.*; public class JSONEncodingTest1 { public static void main(String[] args) throws IOException { JSONObject obj = new JSONObject(); obj.put("Name", "Jai"); obj.put("Mobile_Number", new Integer(995998480)); obj.put("Bank_Balance", new Double(50000.00)); obj.put("Is_A_SelfEmployee", new Boolean(false)); StringWriter out = new StringWriter(); obj.writeJSONString(out); String jsonText = out.toString(); System.out.print(jsonText); } }
Output
{"Is_A_SelfEmployee":false,"Bank_Balance":50000.0,"Mobile_Number":995998480,"Name":"Jai"}
- Related Articles
- How can we decode a JSON object in Java?
- How can we convert a JSON string to a JSON object in Java?
- How can we parse a nested JSON object in Java?
- How can we convert a map to the JSON object in Java?
- How can we read a JSON file in Java?
- How can we check if a JSON object is empty or not in Java?\n
- How can we create a JSON using JsonGenerator in Java?
- How can we merge two JSON objects in Java?
- How can we merge two JSON arrays in Java?
- How can we implement a JSON array using Streaming API in Java?
- How can we write JSON objects to a file in Java?\n
- How can we convert a list to the JSON array in Java?
- How can we change a field name in JSON using Jackson in Java?
- How to construct a JSON object from a subset of another JSON object in Java?
- How can we ignore the fields during JSON serialization in Java?

Advertisements