杰克逊:映射嵌套对象
Jackson : map nested object
使用 jackson,我想知道是否可以使用不同于 json 结构的嵌套对象将 json 映射到 Java。
这是我想做的一个例子。
Json :
{
a = "someValue",
b = "someValue",
c = "someValue"
}
Java :
public class AnObject {
@JsonProperty("a")
private String value;
//Nested object
private SomeObject;
}
public class SomeObject {
@JsonProperty("b")
private String value1;
@JsonProperry("c")
private String value2;
}
可能吗?
首先,这是一个JSON对象。
这是一个 object literal。
其次,这不是有效的格式化对象文字。
正确的是:
{ "a" : "someValue", "b": "someValue", "c": "someValue"}
接下来,如评论中所述,您必须定义自己的反序列化器。
主要:
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{\"a\" : \"someValue\",\"b\" : \"someValue\",\"c\" : \"someValue\"}";
final ObjectMapper om =
new ObjectMapper();//
om.registerSubtypes(AnObject.class);
SimpleModule module = new SimpleModule();
module.addDeserializer(AnObject.class, new CustomDeserializer2());
om.registerModule(module);
AnObject ob = om.readValue(json, AnObject.class);
System.out.println(ob.getValue());
System.out.println(ob.getObject().getValue1());
System.out.println(ob.getObject().getValue2());
}
解串器:
public class CustomDeserializer2 extends StdDeserializer<AnObject> {
private static final long serialVersionUID = -3483096770025118080L;
public CustomDeserializer2() {
this(null);
}
public CustomDeserializer2(Class<?> vc) {
super(vc);
}
@Override
public AnObject deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode interNode = jp.getCodec().readTree(jp);
AnObject ob = new AnObject();
if (interNode.get("a") != null) {
ob.setValue(interNode.get("a").toString());
}
SomeObject obj = new SomeObject();
if (interNode.get("b") != null) {
obj.setValue1(interNode.get("b").toString());
}
if (interNode.get("c") != null) {
obj.setValue2(interNode.get("c").toString());
}
ob.setObject(obj);
return ob;
}
模型:关注A字段上的@JsonProperty
public class AnObject {
@JsonProperty("a")
private String value;
private SomeObject object;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public SomeObject getObject() {
return object;
}
public void setObject(SomeObject arg) {
object = arg;
}
public class SomeObject {
private String value1;
private String value2;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
再见
使用JsonUnwrapped
注释:
@JsonUnwrapped
private final SomeObject someObject;
将 SomeObject
的所有字段解包到父级中,在序列化时产生以下结果:
{"a":"foo","b":"bar","c":"baz"}
使用 ObjectMapper,您可以将 JSON 字符串转换为对象。
在 AnObject class over someObject 字段中使用 JsonUnwrapped。
@JsonUnwrapped
private SomeObject someObject;
然后读取 JSON 字符串并将其转换为 AnObject。
ObjectMapper mapper = new ObjectMapper();
try {
AnObject anObject1 = mapper.readValue(jsonString, AnObject.class);
} catch (IOException e) {
e.printStackTrace();
}
使用 jackson,我想知道是否可以使用不同于 json 结构的嵌套对象将 json 映射到 Java。
这是我想做的一个例子。
Json :
{
a = "someValue",
b = "someValue",
c = "someValue"
}
Java :
public class AnObject {
@JsonProperty("a")
private String value;
//Nested object
private SomeObject;
}
public class SomeObject {
@JsonProperty("b")
private String value1;
@JsonProperry("c")
private String value2;
}
可能吗?
首先,这是一个JSON对象。
这是一个 object literal。
其次,这不是有效的格式化对象文字。 正确的是:
{ "a" : "someValue", "b": "someValue", "c": "someValue"}
接下来,如评论中所述,您必须定义自己的反序列化器。
主要:
public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
String json = "{\"a\" : \"someValue\",\"b\" : \"someValue\",\"c\" : \"someValue\"}";
final ObjectMapper om =
new ObjectMapper();//
om.registerSubtypes(AnObject.class);
SimpleModule module = new SimpleModule();
module.addDeserializer(AnObject.class, new CustomDeserializer2());
om.registerModule(module);
AnObject ob = om.readValue(json, AnObject.class);
System.out.println(ob.getValue());
System.out.println(ob.getObject().getValue1());
System.out.println(ob.getObject().getValue2());
}
解串器:
public class CustomDeserializer2 extends StdDeserializer<AnObject> {
private static final long serialVersionUID = -3483096770025118080L;
public CustomDeserializer2() {
this(null);
}
public CustomDeserializer2(Class<?> vc) {
super(vc);
}
@Override
public AnObject deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
JsonNode interNode = jp.getCodec().readTree(jp);
AnObject ob = new AnObject();
if (interNode.get("a") != null) {
ob.setValue(interNode.get("a").toString());
}
SomeObject obj = new SomeObject();
if (interNode.get("b") != null) {
obj.setValue1(interNode.get("b").toString());
}
if (interNode.get("c") != null) {
obj.setValue2(interNode.get("c").toString());
}
ob.setObject(obj);
return ob;
}
模型:关注A字段上的@JsonProperty
public class AnObject {
@JsonProperty("a")
private String value;
private SomeObject object;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public SomeObject getObject() {
return object;
}
public void setObject(SomeObject arg) {
object = arg;
}
public class SomeObject {
private String value1;
private String value2;
public String getValue1() {
return value1;
}
public void setValue1(String value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
再见
使用JsonUnwrapped
注释:
@JsonUnwrapped
private final SomeObject someObject;
将 SomeObject
的所有字段解包到父级中,在序列化时产生以下结果:
{"a":"foo","b":"bar","c":"baz"}
使用 ObjectMapper,您可以将 JSON 字符串转换为对象。 在 AnObject class over someObject 字段中使用 JsonUnwrapped。
@JsonUnwrapped
private SomeObject someObject;
然后读取 JSON 字符串并将其转换为 AnObject。
ObjectMapper mapper = new ObjectMapper();
try {
AnObject anObject1 = mapper.readValue(jsonString, AnObject.class);
} catch (IOException e) {
e.printStackTrace();
}