例如,我有以下JSON
1
| {age: 18, lastname: "Liu", firstname: "Ryan"}
|
我需要将这个JSON装成Person对象
1
2
3
4
5
6
7
8
| class Person {
private int age;
private Name name;
}
class Name {
private String lastname;
private String firstname;
}
|
只需要使用Jackson的注解@JsonUnwrapped
1
2
3
4
5
| class Person {
private int age;
@JsonUnwrapped
private Name name;
}
|
还有另外一种情况。
1
2
3
4
5
6
7
8
9
10
11
| class Person {
private Id id;
private int age;
}
class Id {
private int val;
public int val() {
return this.val;
}
}
|
转成
这是可使用@JsonValue
注解。
1
2
3
4
5
6
7
8
| class Id {
private int val;
@JsonValue
public int val() {
return this.val;
}
}
|