thorpedosg

Untitled

Jul 24th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. { "0": { "count":"5"},
  2. "1": { "title":"...", "desc":"" },
  3. "2": { "title":"...", "desc":"" },
  4. "3": { "title":"...", "desc":"" },
  5. "4": { "title":"...", "desc":"" },
  6. "5": { "title":"...", "desc":"" },
  7. "routes": { "route1":"...", "route3":"" },
  8. }
  9.  
  10. import java.io.FileReader;
  11. import java.lang.reflect.Type;
  12. import java.util.Map;
  13.  
  14. import com.google.gson.Gson;
  15. import com.google.gson.reflect.TypeToken;
  16.  
  17. public class GsonFoo
  18. {
  19. public static void main(String[] args) throws Exception
  20. {
  21. Gson gson = new Gson();
  22. Type mapType = new TypeToken<Map<String,Map<String, String>>>() {}.getType();
  23. Map<String,Map<String, String>> map = gson.fromJson(new FileReader("input.json"), mapType);
  24. System.out.println(map);
  25.  
  26. // Get the count...
  27. int count = Integer.parseInt(map.get("0").get("count"));
  28.  
  29. // Get each numbered entry...
  30. for (int i = 1; i <= count; i++)
  31. {
  32. System.out.println("Entry " + i + ":");
  33. Map<String, String> numberedEntry = map.get(String.valueOf(i));
  34. for (String key : numberedEntry.keySet())
  35. System.out.printf("key=%s, value=%sn", key, numberedEntry.get(key));
  36. }
  37.  
  38. // Get the routes...
  39. Map<String, String> routes = map.get("routes");
  40.  
  41. // Get each route...
  42. System.out.println("Routes:");
  43. for (String key : routes.keySet())
  44. System.out.printf("key=%s, value=%sn", key, routes.get(key));
  45. }
  46. }
  47.  
  48. import java.io.File;
  49. import java.util.Map;
  50.  
  51. import org.codehaus.jackson.map.ObjectMapper;
  52.  
  53. public class JacksonFoo
  54. {
  55. public static void main(String[] args) throws Exception
  56. {
  57. ObjectMapper mapper = new ObjectMapper();
  58. Map map = mapper.readValue(new File("input.json"), Map.class);
  59. System.out.println(map);
  60. }
  61. }
Add Comment
Please, Sign In to add comment