1

我有以下 json 数据

{"id":10606,
 "name":"ProgrammerTitle",
 "objectMap":{"programme-title":"TestProgramme","working-title":"TestProgramme"}
}

我想将此数据设置为我的 pojo 对象

public class TestObject {
    private Long id;
    private String name;

    @JsonProperty("programme-title")
    private String programmeTitle;

    @JsonProperty("working-title")
    private String workingTitle;
}

在这里,我可以在我的测试对象中设置 id 和 name,但对于对象映射,我无法设置数据。

所以我为包含 programTitle 和 workingTitle 的 ObjectMap 制作了更多类,这工作正常,但我不能将此字段直接设置为我的 pojo 对象,这可以设置吗?

我正在使用 Jackson Object Mapper 来转换 json 数据。

如果我在我的 pojo 中创建另一个 java 对象,它工作正常,例如:

public class TestObject {
private Long id;
private String name;

@JsonProperty("objectMap")
private ObjectMap objectMap;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public ObjectMap getObjectMap() {
    return objectMap;
}

public void setObjectMap(ObjectMap objectMap) {
    this.objectMap = objectMap;
}

}

public class ObjectMap {

@JsonProperty("programme-title")
private String programmeTitle;

@JsonProperty("working-title")
private String workingTitle;

public String getProgrammeTitle() {
    return programmeTitle;
}

public void setProgrammeTitle(String programmeTitle) {
    this.programmeTitle = programmeTitle;
}

public String getWorkingTitle() {
    return workingTitle;
}

public void setWorkingTitle(String workingTitle) {
    this.workingTitle = workingTitle;
}

}

4

2 回答 2

2

如果你的 JSON 是这样的

{"id":10606,
  "name":"ProgrammerTitle",
  "objectMap":{"programme-title":"TestProgramme","working-title":"TestProgramme"}
}

那么你可以像这样编写你的对象映射器类..

public class Program{

    public static class ObjectMap{
     private String programme_title, working_title;

      public String getprogramme_title() { return programme_title; }
     public String getworking_title() { return working_title; }

     public void setprogramme_title(String s) { programme_title= s; }
     public void setworking_title(String s) { working_title= s; }
   }

   private ObjectMap objMap;

   private String name;

   public ObjectMap getobjectMap () { return objMap; }
   public void setObjectMap  (ObjectMap n) { objMap= n; }

   private Long id;
   public Long getId() {return id;}
   public void setId(Long id) {this.id = id;}

   private String name;
   public String getName() {return name;}
   public void setName(String name) {this.name = name;}
  }

请参考此检查

于 2013-10-01T07:33:45.733 回答
1

您可以为此类编写自己的反序列化器:

class EntityJsonDeserializer extends JsonDeserializer<Entity> {

    @Override
    public Entity deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        Root root = jp.readValueAs(Root.class);

        Entity entity = new Entity();
        entity.setId(root.id);
        entity.setName(root.name);
        if (root.objectMap != null) {
            entity.setProgrammeTitle(root.objectMap.programmeTitle);
            entity.setWorkingTitle(root.objectMap.workingTitle);
        }

        return entity;
    }

    private static class Root {
        public Long id;
        public String name;
        public Title objectMap;
    }

    private static class Title {
        @JsonProperty("programme-title")
        public String programmeTitle;

        @JsonProperty("working-title")
        public String workingTitle;
    }
}

您的实体:

@JsonDeserialize(using = EntityJsonDeserializer.class)
class Entity {

    private Long id;
    private String name;
    private String programmeTitle;
    private String workingTitle;

    //getters, setters, toString
}

以及用法示例:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

public class JacksonProgram {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Entity entity = mapper.readValue(jsonString, Entity.class);
        System.out.println(entity);
    }
}

上面的程序打印:

Entity [id=10606, name=ProgrammerTitle, programmeTitle=TestProgramme, workingTitle=TestProgramme]
于 2013-10-01T13:40:14.450 回答