Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. {
  2. "orderID": 12345,
  3. "shopperName": "Ваня Иванов",
  4. "shopperEmail": "ivanov@example.com",
  5. "contents": [
  6. {
  7. "type": "Image",
  8. "Url": "https://someurl.com",
  9. },
  10. {
  11. "type": "Text",
  12. "text": "some text",
  13. "isItalic": true,
  14. "isBold": true
  15. }
  16. ],
  17. "orderCompleted": true
  18. }
  19.  
  20. public class RawCollectionsExample {
  21. static class Event {
  22. private String name;
  23. private String source;
  24. private Event(String name, String source) {
  25. this.name = name;
  26. this.source = source;
  27. }
  28. @Override
  29. public String toString() {
  30. return String.format("(name=%s, source=%s)", name, source);
  31. }
  32. }
  33.  
  34. @SuppressWarnings({ "unchecked", "rawtypes" })
  35. public static void main(String[] args) {
  36. Gson gson = new Gson();
  37. Collection collection = new ArrayList();
  38. collection.add("hello");
  39. collection.add(5);
  40. collection.add(new Event("GREETINGS", "guest"));
  41. String json = gson.toJson(collection);
  42. System.out.println("Using Gson.toJson() on a raw collection: " + json);
  43. JsonParser parser = new JsonParser();
  44. JsonArray array = parser.parse(json).getAsJsonArray();
  45. String message = gson.fromJson(array.get(0), String.class);
  46. int number = gson.fromJson(array.get(1), int.class);
  47. Event event = gson.fromJson(array.get(2), Event.class);
  48. System.out.printf("Using Gson.fromJson() to get: %s, %d, %s", message, number, event);
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement