Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package event;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class EventParser {
  12.  
  13. private static final String fileName = "log.json";
  14.  
  15.  
  16. public static void main(String[] args) {
  17. try {
  18. List<Event> eventsFromFile = readEventsFromFile();
  19. System.out.println(eventsFromFile);
  20. } catch (IOException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25. private static List<Event> readEventsFromFile() throws IOException {
  26. List<Event> eventsFromFile = new ArrayList<>();
  27. ObjectMapper objectMapper = new ObjectMapper();
  28. BufferedReader bufferedReader = new BufferedReader(new FileReader(EventParser.fileName));
  29. while (bufferedReader.readLine() != null) {
  30. Event event = objectMapper.readValue(fileName, Event.class);
  31. eventsFromFile.add(event);
  32. }
  33. return eventsFromFile;
  34. }
  35. }
  36.  
  37. [
  38. {
  39. "id": "scsmbstgra",
  40. "state": "STARTED",
  41. "type": "APPLICATION_LOG",
  42. "host": "12345",
  43. "timestamp": 1491377495212
  44. },
  45. {
  46. "id": "scsmbstgrb",
  47. "state": "STARTED",
  48. "timestamp": 1491377495213
  49. },
  50. {
  51. "id": "scsmbstgrc",
  52. "state": "FINISHED",
  53. "timestamp": 1491377495218
  54. },
  55. {
  56. "id": "scsmbstgra",
  57. "state": "FINISHED",
  58. "type": "APPLICATION_LOG",
  59. "host": "12345",
  60. "timestamp": 1491377495217
  61. },
  62. {
  63. "id": "scsmbstgrc",
  64. "state": "STARTED",
  65. "timestamp": 1491377495210
  66. },
  67. {
  68. "id": "scsmbstgrb",
  69. "state": "FINISHED",
  70. "timestamp": 1491377495216
  71. }
  72. ]
  73.  
  74. package event;
  75.  
  76. import java.sql.Timestamp;
  77.  
  78. public class Event {
  79. private String id;
  80. private String state;
  81. private String type;
  82. private String host;
  83. private Timestamp timestamp;
  84.  
  85. public Event() {
  86. }
  87.  
  88. public Event(String id, String state, Timestamp timestamp, String type, String host) {
  89. this.id = id;
  90. this.state = state;
  91. this.timestamp = timestamp;
  92. this.type = type;
  93. this.host = host;
  94. }
  95.  
  96. public String getId() {
  97. return id;
  98. }
  99.  
  100. public void setId(String id) {
  101. this.id = id;
  102. }
  103.  
  104. public String getState() {
  105. return state;
  106. }
  107.  
  108. public void setState(String state) {
  109. this.state = state;
  110. }
  111.  
  112. public Timestamp getTimestamp() {
  113. return timestamp;
  114. }
  115.  
  116. public void setTimestamp(Timestamp timestamp) {
  117. this.timestamp = timestamp;
  118. }
  119.  
  120. public String getType() {
  121. return type;
  122. }
  123.  
  124. public void setType(String type) {
  125. this.type = type;
  126. }
  127.  
  128. public String getHost() {
  129. return host;
  130. }
  131.  
  132. public void setHost(String host) {
  133. this.host = host;
  134. }
  135. }
  136.  
  137. Task :EventParser.main()
  138. com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'log': was expecting ('true', 'false' or 'null')
  139. at [Source: (String)"log.json"; line: 1, column: 4]
  140. at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
  141. at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
  142. at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853)
  143. at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899)
  144. at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:757)
  145. at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141)
  146. at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
  147. at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
  148. at event.EventParser.readEventsFromFile(EventParser.java:30)
  149. at event.EventParser.main(EventParser.java:18)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement