Home > Java > javaTutorial > How to map JSON data using Jackson object model in Java?

How to map JSON data using Jackson object model in Java?

WBOY
Release: 2023-09-01 16:33:06
forward
706 people have browsed it

How to map JSON data using Jackson object model in Java?

The ObjectMapper class provides functionality for converting between Java objects and matching JSON constructs. We can achieve mapping of JSON data represented by an Object Model to a particular Java object using a tree-like data structure that reads and stores the entire JSON content in memory. In the first step, read the JSON data into the JsonNode object then mapped it to another instance by calling the treeToValue() method of ObjectMapper class.

Syntax

public <T> T treeToValue(TreeNode n, Class<T> valueType) throws JsonProcessingException
Copy after login

Example

import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
public class JsonTreeModelDemo {
   public static void main(String[] args) throws JsonProcessingException, IOException {
      String jsonString = "{\"id\":\"102\", \"name\":\"Raja Ramesh\", \"address\":[{\"area\":\"Madhapur\", \"city\":\"Hyderabad\"}]}";
      ObjectMapper objectMapper = new ObjectMapper();
      JsonNode jsonNode = objectMapper.readTree(jsonString);
      User user = objectMapper.treeToValue(jsonNode, User.class);
      System.out.println(user);
   }
}
// User class
class User {
   private int id;
   private String name;
   private Address[] address;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Address[] getAddress() {
      return address;
   }
   public void setAddress(Address[] address) {
      this.address = address;
   }
<strong>   @Override
</strong>   public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append("{");
      sb.append(id).append(",");
      sb.append(name).append(",");
      for(Address address1 : address) {
         sb.append(address1).append(",");
      }
      sb.append("}");
      return sb.toString();
   }
}
// Address class
class Address {
   private String area;
   private String city;
   public String getArea() {
      return area;
   }
   public void setArea(String area) {
      this.area = area;
   }
   public String getCity() {
      return city;
   }
   public void setCity(String city) {
      this.city = city;
   }
<strong>  </strong> @Override<strong>
</strong>   public String toString() {
      StringBuilder sb = new StringBuilder();
      sb.append("{");
      sb.append(area).append(",");
      sb.append(city);
      sb.append("}");
      return sb.toString();
   }
}
Copy after login

输出

{102,Raja Ramesh,{Madhapur,Hyderabad},}
Copy after login

The above is the detailed content of How to map JSON data using Jackson object model in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template