- 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 JSON object to an enum using Jackson in Java?
A JSONObject can parse the text from String to produce a Map kind of an object. An Enum can be used to define a collection of constants, when we need a predefined list of values which do not represent some kind of numeric or textual data then we can use an enum. We can convert a JSON object to an enum using the readValue() method of ObjectMapper class.
In the below example, we can convert/deserialize a JSON object to Java enum using the Jackson library.
Example
import com.fasterxml.jackson.databind.*; public class JSONToEnumTest { public static void main(String arg[]) throws Exception { ObjectMapper mapper = new ObjectMapper(); Employee emp = mapper.readValue("{\"jobType\":\"CONTRACT\"}", Employee.class); System.out.println(emp.getJobType()); } public static class Employee { private JobType jobType; public JobType getJobType() { return jobType; } public void setJobType(JobType jobType) { this.jobType = jobType; } } public enum JobType { PERMANENT, CONTRACT, } }
Output
CONTRACT
- Related Articles
- How to convert Java object to JSON using Jackson library?
- How to convert a JSON to Java Object using the Jackson library in Java?
- Convert CSV to JSON using the Jackson library in Java?
- Convert JSON to/from Map using Jackson library in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- How can we convert a JSON array to a list using Jackson in Java?
- How to ignore a field of JSON object using the Jackson library in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert Java object to JSON using GSON library?
- How to create a JSON using Jackson Tree Model in Java?
- Convert JSON object to Java object using Gson library in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?
- How to convert a bean to JSON object using Exclude Filter in Java?
- How to map the JSON data with Jackson Object Model in Java?

Advertisements