我有一张地图,其中一个键的值是一个对象列表。能够通过builder.enableComplexMapKeySerialization();
序列化密钥,但是这些值没有按预期序列化,因为它们在反序列化而不是对象上返回一个字符串。
以下是序列化的输出
[{“id”:31001,“name”:老师“]},//这是关键
[{“id”:33033,“name”:“student1”},{“id”:34001,“name”:“student2”}]],//这是值列表
我使用了TypeToken<HashMap<Teacher, List<Student>>>
的相关TypeToken但仍然在反序列化而不是对象上返回了一个字符串值。
答案 0 :(得分:18)
JSON由名称/值对组成(其中值侧可以是事物列表)。其名称部分是一个字符串(参见:http://json.org)
您要做的是使用对象作为名称;你不能直接这样做。 JSON对象不能是名称/值对的名称。
如果您阅读documentation for enableComplexMapKeySerialization,它会解释生成的JSON的内容。
它生成的JSON(Map作为JSON数组)将完全反序列化回到您的地图。以下是一个完整的工作示例(Java 7)。
请注意,一旦我从JSON反序列化回Java,我就会迭代地图来获取密钥。这是因为在equals()
中没有hashCode()
和Teacher
被覆盖,就无法创建Teacher
的新实例并让它作为键工作(只有参考值是相比)。
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class App
{
public static void main( String[] args )
{
HashMap<Teacher, List<Student>> map = new HashMap<>();
Teacher t = new Teacher("12345", "Teacher");
Teacher t2 = new Teacher("23456", "Teacher2");
ArrayList<Student> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
list.add(new Student(String.valueOf(i), "Student" + String.valueOf(i)));
}
map.put(t, list);
map.put(t2, list);
GsonBuilder builder = new GsonBuilder();
Gson gson =
builder.enableComplexMapKeySerialization().setPrettyPrinting().create();
Type type = new TypeToken<HashMap<Teacher,List<Student>>>(){}.getType();
String json = gson.toJson(map, type);
System.out.println(json);
System.out.println("\nAfter deserialization:");
HashMap<Teacher, List<Student>> map2 = gson.fromJson(json, type);
for (Teacher t3 : map2.keySet()) {
System.out.println(t3.name);
for (Student s2 : map2.get(t3)) {
System.out.println("\t" + s2.name);
}
}
}
}
class Teacher {
public String id;
public String name;
public Teacher(String id, String name) {
this.id = id;
this.name = name;
}
}
class Student {
public String id;
public String name;
public Student(String id, String name) {
this.id = id;
this.name = name;
}
}
输出:
[
[
{
"id": "12345",
"name": "Teacher"
},
[
{
"id": "0",
"name": "Student0"
},
{
"id": "1",
"name": "Student1"
},
{
"id": "2",
"name": "Student2"
}
]
],
[
{
"id": "23456",
"name": "Teacher2"
},
[
{
"id": "0",
"name": "Student0"
},
{
"id": "1",
"name": "Student1"
},
{
"id": "2",
"name": "Student2"
}
]
]
]
After deserialization:
Teacher2
Student0
Student1
Student2
Teacher
Student0
Student1
Student2
如果您在equals()
课程中实施hashCode()
和Teacher
,那么您就可以使用Teacher
的新实例从地图中检索内容:< / p>
class Teacher {
public String id;
public String name;
public Teacher(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public int hashCode()
{
int hash = 3;
hash = 37 * hash + Objects.hashCode(this.id);
hash = 37 * hash + Objects.hashCode(this.name);
return hash;
}
@Override
public boolean equals(Object obj)
{
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Teacher other = (Teacher) obj;
if (!Objects.equals(this.id, other.id))
{
return false;
}
if (!Objects.equals(this.name, other.name))
{
return false;
}
return true;
}
}
一旦你有了,你可以这样做:
...
HashMap<Teacher, List<Student>> map2 = gson.fromJson(json, type);
Teacher t = new Teacher("23456", "Teacher2");
List<Student> list = map2.get(t);
...