繁体   English   中英

将 JSON 解析为 Java Object 给了我一些 Z37A6259CC0C1DAFAE299ZA76 值

[英]Parsing JSON as Java Object is giving me some null values

I'm trying to convert a JSON object into a POJO class, but some values are ending up with null values while others are set to the value from the JSON. 回应是

{
"intention_id": 12936,
"intention_datetime": "2021-03-09T09:58:16-03:00",
"status": "created",
"cuit": "20-314559999-9",
"branch": "Avellaneda 13",
"checkout": "8",
"terminal_number": "87980012",
"intention_type": "payment",
"original_amount": 1200.0,
"currency": "ARS",
"payment_methods_data": [
    {
        "payment_method_id": 1,
        "establishment_id": "29880765",
        "trace_number": 1090,
        "ticket_number": 131,
        "installments": [
            {
                "quantity": 1,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "bank_id": 28,
                "quantity": 6,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "quantity": 6,
                "total_amount": 1400.0,
                "amount_per_installment": 233.33,
                "cft": 12.56,
                "tna": 14.87
            }
        ]
    },
    {
        "payment_method_id": 15,
        "establishment_id": "69880548",
        "trace_number": 2034,
        "ticket_number": 673,
        "installments": [
            {
                "quantity": 1,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "bank_id": 28,
                "quantity": 6,
                "total_amount": 1200.0,
                "amount_per_installment": 200.0,
                "cft": 12.56,
                "tna": 14.87
            },
            {
                "quantity": 6,
                "total_amount": 1400.0,
                "amount_per_installment": 233.33,
                "cft": 12.56,
                "tna": 14.87
            }
        ]
    }
],
"integrator_intention_id": "587cba61-ff27-46c6-ba8b-fd936665c35f",
"integrator": "alsea"
}

我忽略了数组元素,所以我的 Object Class 是

    public class ObjectResponseBimo {
    
    public int intention_id;    
    public String intention_datetime;   
    public String status;   
    public String cuit;
    public String branch;   
    public String checkout; 
    public String terminal_number;  
    public String intention_type;   
    public double original_amount;  
    public String currency; 
    public String integrator_intention_id;  
    public String integrator;

    public int getIntention_id() {
        return intention_id;
    }
    @JsonProperty("intention_id")
    public void setIntention_id(int intention_id) {
        this.intention_id = intention_id;
    }
    public String getIntention_datetime() {
        return intention_datetime;
    }
    @JsonProperty("intention_datetime")
    public void setIntention_datetime(String intention_datetime) {
        this.intention_datetime = intention_datetime;
    }
    public String getStatus() {
        return status;
    }
    @JsonProperty("status")
    public void setStatus(String status) {
        this.status = status;
    }
    public String getCuit() {
        return cuit;
    }
    @JsonProperty("cuit")
    public void setCuit(String cuit) {
        this.cuit = cuit;
    }
    public String getBranch() {
        return branch;
    }
    @JsonProperty("branch")
    public void setBranch(String branch) {
        this.branch = branch;
    }
    public String getCheckout() {
        return checkout;
    }
    @JsonProperty("checkout")
    public void setCheckout(String checkout) {
        this.checkout = checkout;
    }
    public String getTerminal_number() {
        return terminal_number;
    }
    @JsonProperty("terminal_number")
    public void setTerminal_number(String terminal_number) {
        this.terminal_number = terminal_number;
    }
    public String getIntention_type() {
        return intention_type;
    }
    @JsonProperty("intention_type")
    public void setIntention_type(String intention_type) {
        this.intention_type = intention_type;
    }
    public double getOriginal_amount() {
        return original_amount;
    }
    @JsonProperty("original_amount")
    public void setOriginal_amount(double original_amount) {
        this.original_amount = original_amount;
    }
    public String getCurrency() {
        return currency;
    }
    @JsonProperty("currency")
    public void setCurrency(String currency) {
        this.currency = currency;
    }
    public String getIntegrator_intention_id() {
        return integrator_intention_id;
    }
    @JsonProperty("integrator_intention_id")
    public void setIntegrator_intention_id(String integrator_intention_id) {
        this.integrator_intention_id = integrator_intention_id;
    }
    public String getIntegrator() {
        return integrator;
    }
    @JsonProperty("integrator")
    public void setIntegrator(String integrator) {
        this.integrator = integrator;
    }
    
    @Override
    public String toString() {
        return "ObjectResponseBimo [intention_id=" + intention_id + ", intention_datetime=" + intention_datetime
                + ", status=" + status + ", cuit=" + cuit + ", branch=" + branch + ", checkout=" + checkout
                + ", terminal_number=" + terminal_number + ", intention_type=" + intention_type + ", original_amount="
                + original_amount + ", currency=" + currency + ", integrator_intention_id=" + integrator_intention_id
                + ", integrator=" + integrator + "]";
    }}

所有其他的 getter 和 setter,每个人的 set 方法中都有 JsonProperty

我正在使用 Jackson,所以我发送 object

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
ObjectResponseBimo o = objectMapper.readValue(jsonToPost, ObjectResponseBimo.class);
return objectMapper.toString() + "\n" + o.toString();

output 是:

ObjectResponseBimo [intention_id=0,intention_datetime=null,status=null,cuit=20-314559999-9,branch=Avellaneda 13,结帐=8,terminal_number=87980012,intention_type=payment,original_amount=1200.0,currency=ARS,integrator_intention_id=587cba61 -ff27-46c6-ba8b-fd936665c35f,积分器=空]

因此,我只是尝试复制您的代码并进行了一些更改。

  1. 在您的 pojo class (ObjectResponseBimo.java) 顶部添加以下注释
  **@JsonIgnoreProperties(ignoreUnknown = true)**
public class ObjectResponseBimo {

这将忽略 json 中与等效 java 属性不匹配的未知属性。

  1. 对于 object 映射器,下面的代码片段应该足以让它工作:
ObjectMapper objectMapper = new ObjectMapper();
ObjectResponseBimo ob = objectMapper.readValue(ClassLoader.getSystemResourceAsStream("ObjectResponseBimo.json"), ObjectResponseBimo.class);
System.out.println(ob.toString());
System.out.println(ob.getIntention_id() + " | " + ob.getBranch());

我不确定为什么 toString() 不适合你。 虽然它对我有用。

您提供的代码工作正常。 我刚刚将它复制到干净的项目中并进行了测试。

我会给你几个笔记,与问题无关。

  • Class 变量应该是private的,对于 Jackson 不需要设置器和获取器来生成 json。 它使用反射。
  • Class 变量应该是CamelCase 这是 Java 中的一般做法。

这就是,您的数据 class 应该如下所示:

public class ObjectResponseBimo {

    @JsonProperty("intention_id")
    private int intentionId;
    @JsonProperty("intention_datetime")
    private String intentionDatetime;
    private String status;
    private String cuit;
    private String branch;
    private String checkout;
    @JsonProperty("terminal_number")
    private String terminalNumber;
    @JsonProperty("intention_type")
    private String intentionType;
    @JsonProperty("original_amount")
    private double originalAmount;
    private String currency;
    @JsonProperty("integrator_intention_id")
    private String integratorIntentionId;
    private String integrator;
}

要使用 json,您可以使用一个工具来隐藏所有常规。 例如JacksonUtils

ObjectResponseBimo obj = JacksonUtils.readValue(json, ObjectResponseBimo.class);

PS重复,您提供的代码在干净的项目上运行良好。 可能你在其他地方有问题,这只是副作用

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM