
- 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 into an existing object in Java?
The Flexjson is a lightweight java library for serializing and deserializing java beans, maps, arrays, and collections in JSON format. We can also deserialize a JSON string to an existing object using the deserializeInto() method of JSONDeserializer class, this method deserializes the given input into the existing object target. The values in the json input can overwrite values in the target object. This means if a value is included in JSON a new object can be created and set into the existing object.
Syntax
public T deserializeInto(String input, T target)
Example
import flexjson.JSONDeserializer; public class JsonDeserializeTest { public static void main(String[] args) { Employee emp = new Employee("Adithya", "Ram", 25, 35000.00); System.out.println(emp); JSONDeserializer<Employee> deserializer = new JSONDeserializer<Employee>(); String jsonStr = "{" + "\"age\": 30," + "\"salary\": 45000.00" + "}"; emp = deserializer.deserializeInto(jsonStr, emp); System.out.println(emp); } } // Employee class class Employee { private String firstName; private String lastName; private int age; private double salary; public Employee() {} public Employee(String firstName, String lastName, int age, double salary) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.salary = salary; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } public String toString() { return "Employee[ " + "firstName = " + firstName + ", lastName = " + lastName + ", age = " + age + ", salary = " + salary + " ]"; } }
Output
Employee[ firstName = Adithya, lastName = Ram, age = 25, salary = 35000.0 ] Employee[ firstName = Adithya, lastName = Ram, age = 30, salary = 45000.0 ]
- Related Articles
- How to deserialize a JSON into Javascript object?
- How to deserialize a JSON to Java object using the flexjson in Java?
- How to add a JSON string to an existing JSON file in Java?
- How to serialize and deserialize an object in Java?
- How to deserialize a JSON string using @JsonCreator annotation in Java?
- How to deserialize a JSON array to list generic type in Java?\n
- How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?
- How to deserialize a Java object from Reader Stream using flexjson in Java?
- How to serialize a Polyline object into JSON in FabricJS?
- How to convert JSON data into a Python object?
- How to transform JSON text into a JavaScript object?
- How to construct a JSON object from a subset of another JSON object in Java?
- How to convert a JSON string into a JavaScript object?
- How to convert a JSON object to an enum using Jackson in Java?
- How can we update an existing JSON data using javax.json API in Java?

Advertisements