问题描述
我有以下json数据
{"id":10606,
"name":"ProgrammerTitle",
"objectMap":{"programme-title":"TestProgramme","working-title":"TestProgramme"}
}
我想将此数据设置为我的pojo对象
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;
}
这里我可以在我的测试对象中设置id和name但是对象map我无法设置数据。
Here i am able to set id and name in my test object but for object map i am not able to set data.
所以我在ObjectMap上创建了更多类,其中包含programmeTitle& workingTitle这个工作正常但是我不能直接将这个字段设置为我的pojo对象
这可以设置吗?
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 objectis this possible to set?
我正在使用Jackson Object Mapper进行转换json data。
I am using Jackson Object Mapper to convert json data.
如果我在我的pojo中创建另一个java对象,它工作正常,如:
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;
}
}
推荐答案
如果您的JSON是这样的
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;}
}
请参阅此
这篇关于使用jackson将Json Object转换为java对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!