fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import com.google.gson.Gson;
  7. import com.google.gson.GsonBuilder;
  8. import java.io.FileReader;
  9. import java.lang.reflect.Type;
  10. import java.util.Map;
  11.  
  12. import com.google.gson.Gson;
  13. import com.google.gson.reflect.TypeToken;
  14.  
  15. public class GsonFoo
  16. {
  17. public static void main(String[] args) throws Exception
  18. {
  19. Gson gson = new Gson();
  20. Type mapType = new TypeToken<Map<String,Map<String, String>>>() {}.getType();
  21. Map<String,Map<String, String>> map = gson.fromJson(new FileReader("input.json"), mapType);
  22. System.out.println(map);
  23.  
  24. // Get the count...
  25. int count = Integer.parseInt(map.get("0").get("count"));
  26.  
  27. // Get each numbered entry...
  28. for (int i = 1; i <= count; i++)
  29. {
  30. System.out.println("Entry " + i + ":");
  31. Map<String, String> numberedEntry = map.get(String.valueOf(i));
  32. for (String key : numberedEntry.keySet())
  33. System.out.printf("key=%s, value=%s\n", key, numberedEntry.get(key));
  34. }
  35.  
  36. // Get the routes...
  37. Map<String, String> routes = map.get("routes");
  38.  
  39. // Get each route...
  40. System.out.println("Routes:");
  41. for (String key : routes.keySet())
  42. System.out.printf("key=%s, value=%s\n", key, routes.get(key));
  43. }
  44. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:15: error: class GsonFoo is public, should be declared in a file named GsonFoo.java
public class GsonFoo
       ^
Main.java:6: error: package com.google.gson does not exist
import com.google.gson.Gson;
                      ^
Main.java:7: error: package com.google.gson does not exist
import com.google.gson.GsonBuilder;
                      ^
Main.java:12: error: package com.google.gson does not exist
import com.google.gson.Gson;
                      ^
Main.java:13: error: package com.google.gson.reflect does not exist
import com.google.gson.reflect.TypeToken;
                              ^
Main.java:19: error: cannot find symbol
    Gson gson = new Gson();
    ^
  symbol:   class Gson
  location: class GsonFoo
Main.java:19: error: cannot find symbol
    Gson gson = new Gson();
                    ^
  symbol:   class Gson
  location: class GsonFoo
Main.java:20: error: cannot find symbol
    Type mapType = new TypeToken<Map<String,Map<String, String>>>() {}.getType();
                       ^
  symbol:   class TypeToken
  location: class GsonFoo
8 errors
stdout
Standard output is empty