使用jackson将Json Object转换为java对象

时间:2013-10-01 06:49:25

标签: java json jackson

我有以下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,但对于对象图,我无法设置数据。

所以我在ObjectMap上创建了更多类,其中包含programmeTitle& workingTitle这个工作正常但我不能直接将这个字段设置到我的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;
}

}

2 个答案:

答案 0 :(得分: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;}
  }

请参阅此check it

答案 1 :(得分: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]