java 使用 Gson 反序列化 Map<String, Object> 字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14263114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deserializing a Map<String, Object> field with Gson
提问by Franck Yapadesouci Anso
I have a User object with this structure:
我有一个具有这种结构的 User 对象:
class User {
private String id;
private String name;
private Map<String, Object> properties;
// GETTERS & SETTERS
}
I have a JSON String with this structure:
我有一个具有以下结构的 JSON 字符串:
{
"user": {
"id:"123456789",
"name:"azerty",
"emailHash":"123456789", // not used in User class
"properties": {
"p1":1,
"p2":"test",
"p3":[1, 2, 3, 4],
"p4":{
etc...
}
}
}
}
Properties' keys are String, Properties' values can be a String, int, Array, boolean, Map etc.
属性的键是字符串,属性的值可以是字符串、整数、数组、布尔值、映射等。
I try to deserialize this JSON string with Gson like that:
我尝试使用 Gson 反序列化这个 JSON 字符串:
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(jsonString);
JsonObject object = element.getAsJsonObject();
GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
Gson gson = builder.create();
User user = (User) gson.fromJson(object.get("user"), new TypeToken<User>() {}.getType());
Fields 'id' and 'name' are correctly injected but the field 'properties' stays null.
字段 'id' 和 'name' 被正确注入,但字段 'properties' 保持为空。
Do you know what I'm doing wrong? Thanks in advance for your help!
你知道我做错了什么吗?在此先感谢您的帮助!
采纳答案by Boris Strandjev
For me this code:
对我来说这个代码:
public class Main {
public static void main(String[] args) throws IOException {
GsonBuilder builder = new GsonBuilder();
builder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
Gson gson = builder.create();
FileInputStream inputStream = new FileInputStream(new File("bobi.json"));
InputStreamReader reader = new InputStreamReader(inputStream);
User user = gson.fromJson(reader, User.class);
System.out.println(user.getName());
System.out.println(user.getId());
for (String property : user.getProperties().keySet()) {
System.out.println("Key: " + property + " value: " + user.getProperties().get(property));
}
reader.close();
}
Prints this:
打印这个:
azerty
123456789
Key: p1 value: 1.0
Key: p2 value: test
Key: p3 value: [1.0, 2.0, 3.0, 4.0]
Key: p4 value: {}
However, keep in mind that I have stripped the wrapping json object in the file I parse. The file is:
但是,请记住,我已经在解析的文件中剥离了包装 json 对象。该文件是:
{
"id":"123456789",
"name" : "azerty",
"emailHash":"123456789",
"properties": {
"p1":1,
"p2":"test",
"p3":[1, 2, 3, 4],
"p4":{
}
}
}
Also I added closing double quote for name
and id
, which you did not have in your sample.
我还为name
and添加了结束双引号id
,您的示例中没有。
The User
class as requested by the OP. I have added getters and setters for the reason of printing:
User
OP 要求的课程。由于打印的原因,我添加了 getter 和 setter:
import java.util.Map;
class User {
private String id;
private String name;
private Map<String, Object> properties;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, Object> getProperties() {
return properties;
}
public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}
}