本文介绍了Gson序列化HashMap<教师,列表<学生>>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个地图,其中一个键的值是一个对象列表。能够通过 builder.enableComplexMapKeySerialization(); 序列化这些键,但这些值没有按预期序列化,因为它们在反序列化而不是对象上返回一个字符串。 b $ b 以下是序列化的输出 我使用了相关的TypeToken,它是 TypeToken< HashMap< Teacher,List< Student>>>>< / code>但仍然列表值在反序列化而不是对象上返回一个字符串。 )解决方案 a href =http://json.org =noreferrer> http://json.org ) 你正在尝试什么要做的是使用o作为名字的对象;你不能直接这样做。 JSON对象不能是名称/值对的名称。 如果您阅读 enableComplexMapKeySerialization的文档,它解释了结果JSON将会是什么。 它产生的JSON(一个Map作为JSON数组)将完全反序列化回你的地图。以下是一个完整的工作示例(Java 7)。 请注意,一旦我从JSON反序列化为Java,我将遍历映射以获取密钥。这是因为在教师中覆盖 equals()和 hashCode() c $ c>没有办法创建 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<>(); 教师t =新教师(12345,教师); 教师t2 =新教师(23456,教师2); ArrayList< Student> list = new ArrayList<>(); (学生)+ String.valueOf(i))); for(int i = 0; i list.add(new Student(String.valueOf(i),Student } 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(\\\Ars after deserialization:); HashMap< Teacher,List< Student>> map2 = gson.fromJson(json,type); (教师t3:map2.keySet()){ System.out.println(t3.name); (学生s2:map2.get(t3)){ System.out.println(\ t+ s2.name); } } } } 班级老师{ public String id; 公共字符串名称; public Teacher(String id,String name){ this.id = id; this.name = name; } } class Student { public String id; 公共字符串名称; 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} ] ] ] 反序列化后:教师2 学生0 学生1 学生2 教师学生0 学生1 梭哈ent2 如果您实现 equals()并且在你的 Teacher 类中 hashCode(),你就可以使用一个新的 Teacher 从地图中检索: class Teacher { 公共字符串ID; 公共字符串名称; 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); 返回散列; } @Override public boolean equals(Object obj) { if(obj == null) {返回false; if(getClass()!= obj.getClass()) { return false; } 最后教师其他=(教师)obj; if(!Objects.equals(this.id,other.id)) { return false; if(!Objects.equals(this.name,other.name)) { return false; } 返回true; } } 一旦你有了,你可以做: ... HashMap< Teacher,List< Student>> map2 = gson.fromJson(json,type); 教师t =新教师(23456,教师2); 列表< Student> list = map2.get(t); ... I have a map where the a key's value is a list of objects. Am able to serialize the keys through builder.enableComplexMapKeySerialization(); but the values are not serialized as expected because they return a string on deserialization instead of object.Below is the output of serializationI used the relevant TypeToken which is TypeToken<HashMap<Teacher, List<Student>>> but still the list values is returned a string on deserialization instead of object. 解决方案 JSON is made up of name/value pairs (where the value side can be a list of things). The name part of that is a string (See: http://json.org)What you're trying to do is use an object as a name; you can't do that directly. A JSON object can't be a name for a name/value pair.If you read the documentation for enableComplexMapKeySerialization it explains what the resulting JSON is going to be.The JSON it produces (a Map as a JSON array) will deserialize perfectly back to your map. The following is a complete, working example (Java 7).Note that once I deserialize from JSON back to Java, I'm iterating over the map to get the keys. This is because without equals() and hashCode() being overridden in Teacher there's no way to create a new instance of Teacher and have it work as a key (only the reference values are compared). 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; }}Output:[ [ { "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 Student2Teacher Student0 Student1 Student2If you implement equals() and hashCode() in your Teacher class, you would then be able to use a new instance of Teacher to retrieve things from the map: 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; }}Once you have that, you could do:...HashMap<Teacher, List<Student>> map2 = gson.fromJson(json, type);Teacher t = new Teacher("23456", "Teacher2");List<Student> list = map2.get(t);... 这篇关于Gson序列化HashMap<教师,列表<学生>>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-25 11:38