Map/access Pojo 对象内部数组

Map/access array inside object on Pojo

如何在 Pojo 上访问和映射对象内的数组 Spring Boot?

这是我想要映射的

{
    "customers": [
        {
            "customerId": "0434556574",
            "paymentCode": "90501"
        }
    ]
}

您需要 类 来匹配 JSON 结构:

public class CustomersData {
    private List<Customer> customers;

    // Constructor, getter and setter
}
public class Customer {
    private String customerId;
    private String paymentCode;

    // Constructor, getter and setter
}

重要的是确保您的 JSON 属性名称与 Java 类 中的属性名称匹配(从而避免它们之间的显式映射)。

您好有很多实用程序库,您可以将 JSON 转换为所需的 java class,以便您可以以正确的方式实例化。 基于您输入的示例:

------------------------------------com.example.Customer.java----- ------------------------------

package com.example;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"customerId",
"paymentCode"
})
@Generated("jsonschema2pojo")
public class Customer {

@JsonProperty("customerId")
private String customerId;
@JsonProperty("paymentCode")
private String paymentCode;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("customerId")
public String getCustomerId() {
return customerId;
}

@JsonProperty("customerId")
public void setCustomerId(String customerId) {
this.customerId = customerId;
}

@JsonProperty("paymentCode")
public String getPaymentCode() {
return paymentCode;
}

@JsonProperty("paymentCode")
public void setPaymentCode(String paymentCode) {
this.paymentCode = paymentCode;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"customers"
})
@Generated("jsonschema2pojo")
public class Example {

@JsonProperty("customers")
private List<Customer> customers = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

@JsonProperty("customers")
public List<Customer> getCustomers() {
return customers;
}

@JsonProperty("customers")
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}

参考:jsontopojo