Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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: <a href="http://json.org" rel="noreferrer">http://json.org</a>)</p> <p>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. </p> <p>If you read the <a href="http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/GsonBuilder.html#enableComplexMapKeySerialization()" rel="noreferrer">documentation for enableComplexMapKeySerialization</a> it explains what the resulting JSON is going to be.</p> <p>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). </p> <p>Note that once I deserialize from JSON back to Java, I'm iterating over the map to get the keys. This is because without <code>equals()</code> and <code>hashCode()</code> being overridden in <code>Teacher</code> there's no way to create a new instance of <code>Teacher</code> and have it work as a key (only the reference values are compared). </p> <pre><code>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&lt;Teacher, List&lt;Student&gt;&gt; map = new HashMap&lt;&gt;(); Teacher t = new Teacher("12345", "Teacher"); Teacher t2 = new Teacher("23456", "Teacher2"); ArrayList&lt;Student&gt; list = new ArrayList&lt;&gt;(); for (int i = 0; i &lt; 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&lt;HashMap&lt;Teacher,List&lt;Student&gt;&gt;&gt;(){}.getType(); String json = gson.toJson(map, type); System.out.println(json); System.out.println("\nAfter deserialization:"); HashMap&lt;Teacher, List&lt;Student&gt;&gt; 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; } } </code></pre> <p>Output:</p> <pre><code>[ [ { "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 </code></pre> <p>If you implement <code>equals()</code> and <code>hashCode()</code> in your <code>Teacher</code> class, you would then be able to use a new instance of <code>Teacher</code> to retrieve things from the map: </p> <pre><code>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; } } </code></pre> <p>Once you have that, you could do:</p> <pre><code>... HashMap&lt;Teacher, List&lt;Student&gt;&gt; map2 = gson.fromJson(json, type); Teacher t = new Teacher("23456", "Teacher2"); List&lt;Student&gt; list = map2.get(t); ... </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload