1

I have following json data

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

I want to set this data to my pojo object

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

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

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

Here i am able to set id and name in my test object but for object map i am not able to set data.

So i have made on more class for ObjectMap which contains programmeTitle & workingTitle this works fine but i can't set this fields directly to my pojo object is this possible to set?

I am using Jackson Object Mapper to convert json data.

It is working fine if i create another java object inside my pojo like:

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
  • check out this link mkyong.com/java/how-to-convert-java-object-to-from-json-jackson Commented Oct 1, 2013 at 6:56
  • 1
    Your json object and Java POJO does not match. TestObject->programme-title is not same as Obj->objectMap->programme-title Commented Oct 1, 2013 at 6:57
  • Hi i have checked the post given i get it but i want to use json object which is inside that object is this possible ? Commented Oct 1, 2013 at 7:10
  • It is not possibel to define this kind of mapping using annotations. But you can implement your custom deserializer for this type and use it in deserialization process. See this link: javacodegeeks.com/2013/08/… Commented Oct 1, 2013 at 11:58

2 Answers 2

2

If your JSON is like this

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

then you may write your object mapper class like this..

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;}
  }

please refer this check it

Sign up to request clarification or add additional context in comments.

1 Comment

hi i want to set programmeTitle directly which is inside TestObject.I don't want to use another ObjectMap inside TestObject.
1

You can write your own deserializer for this class:

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;
    }
}

Your entity:

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

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

    //getters, setters, toString
}

And usage example:

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);
    }
}

Above program prints:

Entity [id=10606, name=ProgrammerTitle, programmeTitle=TestProgramme, workingTitle=TestProgramme]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.