Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

วิธีถอดรหัสสตริง JSON ด้วย @JsonCreator ใน Java

Annotation @JsonProperty ใช้ระบุ ชื่อคุณสมบัติ ใน JSON สามารถใช้กับ constructor หรือ factory method Annotation @JsonCreator มีประโยชน์ในสถานการณ์ที่ @JsonSetter ไม่สามารถใช้ได้ เช่น อ็อบเจกต์ที่ไม่เปลี่ยนแปลง (immutable) ไม่มีเมธอด setter จึงต้องฉีดค่าเริ่มต้นผ่าน constructor

@JsonProperty - Constructor

ตัวอย่างโค้ด

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 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 + "]";
    }
}

ผลลัพธ์

JSON: {"id":"101", "fullname":"Ravi Chandra", "location":"Pune"}
Customer [id=101, name=Ravi Chandra, address=Pune]

@JsonCreator - Factory Method

ตัวอย่างโค้ด

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 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 + "]";
    }
}

ผลลัพธ์

JSON: {"id":"101", "fullname":"Raja Ramesh", "location":"Hyderabad"}
Customer [id=102, name=Raja Ramesh, address=Hyderabad]