
- 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 deserialize a JSON string using @JsonCreator annotation in Java?
The @JsonProperty annotation can be used to indicate the property name in JSON. This annotation can be used for a constructor or factory method. The @JsonCreator annotation is useful in situations where the @JsonSetter annotation cannot be used. For instance, immutable objects do not have any setter methods, so they need their initial values injected into the constructor.
@JsonProperty - Constructor
Example
import com.fasterxml.jackson.annotation.*; import java.io.IOException; import com.fasterxml.jackson.databind.*; public class JsonCreatorTest1 { public static void main(String[] args) throws IOException { ObjectMapper om = new ObjectMapper(); String jsonString = "{\"id\":\"101\", \"fullname\":\"Ravi Chandra\", \"location\":\"Pune\"}"; System.out.println("JSON: " + jsonString); Customer customer = om.readValue(jsonString, Customer.class); System.out.println(customer); } } // Customer class class Customer { private String id; private String name; private String address; public Customer() { } @JsonCreator public Customer( @JsonProperty("id") String id, @JsonProperty("fullname") String name, @JsonProperty("location") String address) { this.id = id; this.name = name; this.address = address; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; } }
Output
JSON: {"id":"101", "fullname":"Ravi Chandra", "location":"Pune"} Customer [id=101, name=Ravi Chandra, address=Pune]
@JsonCreator - Factory Method
Example
import com.fasterxml.jackson.annotation.*; import java.io.IOException; import com.fasterxml.jackson.databind.*; public class JsonCreatorTest2 { public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); String jsonString = "{\"id\":\"102\", \"fullname\":\"Raja Ramesh\", \"location\":\"Hyderabad\"}"; System.out.println("JSON: " + jsonString); Customer customer = mapper.readValue(jsonString, Customer.class); System.out.println(customer); } } // Customer class class Customer { private String id; private String name; private String address; public Customer() { } @JsonCreator public static Customer createCustomer( @JsonProperty("id") String id, @JsonProperty("fullname") String name, @JsonProperty("location") String address) { Customer customer = new Customer(); customer.id = id; customer.name = name; customer.address = address; return customer; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; } }
Output
JSON: {"id":"101", "fullname":"Raja Ramesh", "location":"Hyderabad"} Customer [id=102, name=Raja Ramesh, address=Hyderabad]
- Related Articles
- How to deserialize a JSON to Java object using the flexjson in Java?
- How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?
- How to exclude a field from JSON using @Expose annotation in Java?
- How to deserialize a JSON into an existing object in Java?
- How to control serialization through @JSON annotation using flexjson in Java?\n
- How to deserialize a JSON array to list generic type in Java?\n
- How to deserialize a JSON into Javascript object?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to parse a JSON string using Streaming API in Java?
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- How to deserialize a Java object from Reader Stream using flexjson in Java?
- How can we convert a JSON string to a JSON object in Java?
- How to add a JSON string to an existing JSON file in Java?
- How to write a JSON string to file using the Gson library in Java?
- How to implement custom serializer using @JsonSerialize annotation in Java?

Advertisements