How can I access and map an array inside an object on Pojo Spring Boot?
This is what I wanted to map
{
"customers": [
{
"customerId": "0434556574",
"paymentCode": "90501"
}
]
}
CodePudding user response:
You would need to classes to match the JSON structure:
public class CustomersData {
private List<Customer> customers;
// Constructor, getter and setter
}
public class Customer {
private String customerId;
private String paymentCode;
// Constructor, getter and setter
}
Important thing is to make sure your JSON properties name matches the attributes name in your Java classes (thus avoiding explicit mapping between them).
CodePudding user response:
Hi There are lot's of utility libraries you can convert JSON to required java class so that you can instantiate in right way. Example based on your input:
-----------------------------------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);
}
}
Reference :jsontopojo