Java jackson对象属性转换为基元
我有下面的问题Java jackson对象属性转换为基元,java,json,rest,jackson,Java,Json,Rest,Jackson,我有下面的问题 { foo:{ id:1 }, name:'Albert', age: 32 } 如何反序列化到Java Pojo public class User { private int fooId; private String name; private int age; } 您可以执行以下操作之一: 创建表示Foo的具体类型: public class Foo { private int id; .
{
foo:{
id:1
},
name:'Albert',
age: 32
}
如何反序列化到Java Pojo
public class User {
private int fooId;
private String name;
private int age;
}
您可以执行以下操作之一:
- 创建表示
的具体类型:Foo
然后在public class Foo { private int id; ... }
中,您将有:User
public class User { private Foo foo; ... }
- 使用
:地图
公共类用户{ 私人地图馆; ... }
如果其他调用者确实希望您拥有
getFooId
和setFooId
,您仍然可以提供这些方法,然后根据您选择的选项委托给Foo
或Map
。只需确保使用@JsonIgnore
对它们进行注释,因为它们不是真正的属性。您可以使用非常有用的google API
首先,创建以下两个类:
用户类别:
Foo类:
如果您有一个example.json
文件,那么将其反序列化如下
Gson gson = new Gson();
User data = gson.fromJson(new BufferedReader(new FileReader(
"example.json")), new TypeToken<User>() {
}.getType());
Gson gson = new Gson();
User data = gson.fromJson(exampleJson, User.class);
这就是您需要使用构造函数中的
JsonProperty
注释反序列化的内容
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class User {
private int fooId;
private String name;
private int age;
public int getFooId() {
return fooId;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public User(@JsonProperty("age") Integer age, @JsonProperty("name") String name,
@JsonProperty("foo") JsonNode foo) {
this.age = age;
this.name = name;
this.fooId = foo.path("id").asInt();
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"foo\":{\"id\":1}, \"name\":\"Albert\", \"age\": 32}" ;
try {
User user = objectMapper.readValue(json, User.class);
System.out.print("User fooId: " + user.getFooId());
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出:
用户fooId:1
希望有帮助
Jose Luis可能与@Batshray重复这里没有数组,我相信问题在于json中的id嵌套在一个没有POJO的foo对象中。没有类似于@JsonProperty(“foo.id”)的东西?为什么你不能改变你的POJO的结构?有一个例子几乎正是你想要做的。只需在
User
中创建一个内部Foo
类。
Gson gson = new Gson();
User data = gson.fromJson(new BufferedReader(new FileReader(
"example.json")), new TypeToken<User>() {
}.getType());
Gson gson = new Gson();
User data = gson.fromJson(exampleJson, User.class);
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
public class User {
private int fooId;
private String name;
private int age;
public int getFooId() {
return fooId;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public User(@JsonProperty("age") Integer age, @JsonProperty("name") String name,
@JsonProperty("foo") JsonNode foo) {
this.age = age;
this.name = name;
this.fooId = foo.path("id").asInt();
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
String json = "{\"foo\":{\"id\":1}, \"name\":\"Albert\", \"age\": 32}" ;
try {
User user = objectMapper.readValue(json, User.class);
System.out.print("User fooId: " + user.getFooId());
} catch (IOException e) {
e.printStackTrace();
}
}
}