Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package event;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- public class EventParser {
- private static final String fileName = "log.json";
- public static void main(String[] args) {
- try {
- List<Event> eventsFromFile = readEventsFromFile();
- System.out.println(eventsFromFile);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private static List<Event> readEventsFromFile() throws IOException {
- List<Event> eventsFromFile = new ArrayList<>();
- ObjectMapper objectMapper = new ObjectMapper();
- BufferedReader bufferedReader = new BufferedReader(new FileReader(EventParser.fileName));
- while (bufferedReader.readLine() != null) {
- Event event = objectMapper.readValue(fileName, Event.class);
- eventsFromFile.add(event);
- }
- return eventsFromFile;
- }
- }
- [
- {
- "id": "scsmbstgra",
- "state": "STARTED",
- "type": "APPLICATION_LOG",
- "host": "12345",
- "timestamp": 1491377495212
- },
- {
- "id": "scsmbstgrb",
- "state": "STARTED",
- "timestamp": 1491377495213
- },
- {
- "id": "scsmbstgrc",
- "state": "FINISHED",
- "timestamp": 1491377495218
- },
- {
- "id": "scsmbstgra",
- "state": "FINISHED",
- "type": "APPLICATION_LOG",
- "host": "12345",
- "timestamp": 1491377495217
- },
- {
- "id": "scsmbstgrc",
- "state": "STARTED",
- "timestamp": 1491377495210
- },
- {
- "id": "scsmbstgrb",
- "state": "FINISHED",
- "timestamp": 1491377495216
- }
- ]
- package event;
- import java.sql.Timestamp;
- public class Event {
- private String id;
- private String state;
- private String type;
- private String host;
- private Timestamp timestamp;
- public Event() {
- }
- public Event(String id, String state, Timestamp timestamp, String type, String host) {
- this.id = id;
- this.state = state;
- this.timestamp = timestamp;
- this.type = type;
- this.host = host;
- }
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getState() {
- return state;
- }
- public void setState(String state) {
- this.state = state;
- }
- public Timestamp getTimestamp() {
- return timestamp;
- }
- public void setTimestamp(Timestamp timestamp) {
- this.timestamp = timestamp;
- }
- public String getType() {
- return type;
- }
- public void setType(String type) {
- this.type = type;
- }
- public String getHost() {
- return host;
- }
- public void setHost(String host) {
- this.host = host;
- }
- }
- Task :EventParser.main()
- com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'log': was expecting ('true', 'false' or 'null')
- at [Source: (String)"log.json"; line: 1, column: 4]
- at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1804)
- at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:703)
- at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._reportInvalidToken(ReaderBasedJsonParser.java:2853)
- at com.fasterxml.jackson.core.json.ReaderBasedJsonParser._handleOddValue(ReaderBasedJsonParser.java:1899)
- at com.fasterxml.jackson.core.json.ReaderBasedJsonParser.nextToken(ReaderBasedJsonParser.java:757)
- at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4141)
- at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4000)
- at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
- at event.EventParser.readEventsFromFile(EventParser.java:30)
- at event.EventParser.main(EventParser.java:18)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement