我正在整合第三方REST API。我已经为给定的json编写了pojo。表格第三方我以json的形式得到回应
"custom_fields": {
"Field_68092": {
"type": "char",
"required": true,
"value": "1",
"label": "orderId"
}
}
我尝试了以下POJO
public class Field_68092 {
private String type;
private String value;
private String label;
private boolean required;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
}
和自定义文件的pojo是
public class Custom_fields {
private Field_68092 Field_68092;
public Field_68092 getField_68092() {
return Field_68092;
}
public void setField_68092(Field_68092 field_68092) {
Field_68092 = field_68092;
}
}
Field_68092每次都为null。有人能帮我找到POJO结构有什么问题吗?
答案 0 :(得分:1)
我觉得你的POJO没问题。请尝试使用此JSON
{"field_68092":{"value":"1","type":"char","label":"orderId","required":true}}
答案 1 :(得分:0)
如果你使用杰克逊,你应该使用额外的注释
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Field_68092 {
而custom_fields它不是类名 - 它是其他类中的字段名
我检查,一切顺利
付款类:
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Payment {
private Field_68092 custom_fields;
public Field_68092 getCustom_fields() {
return custom_fields;
}
public void setCustom_fields(Field_68092 custom_fields) {
this.custom_fields = custom_fields;
}
}
Field_68092类
import com.fasterxml.jackson.annotation.JsonTypeInfo;
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
public class Field_68092 {
private String type;
private String value;
private String label;
private boolean required;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
}
简单测试:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.StringWriter;
public class Main {
public static void main(String[] args) throws Exception{
Payment p = new Payment();
Field_68092 f = new Field_68092();
f.setLabel("label");
f.setRequired(false);
f.setType("type");
f.setValue("value");
p.setCustom_fields(f);
ObjectMapper m = new ObjectMapper();
StringWriter w = new StringWriter();
m.writer().writeValue(w, p);
System.out.println(w.getBuffer());
String json = "{\"Payment\":{\"custom_fields\":{\"Field_68092\":{\"type\":\"type\",\"value\":\"value\",\"label\":\"label\",\"required\":false}}}}";
Payment p2 = m.readValue(json, Payment.class);
System.out.println(p2.getCustom_fields());
}
}