0

So far I have a tweet.json file which inside is collections of tweet. I have constructed a tweet.class which is a collection of objects from the key-value in the tweet.

now i want to implement a parses using jackson which should run through line-by-line in tweet.json and each tweet (line) is parsed to POJO. So each line is a single POJO

How do I do this

This is what I have done

ObjectMapper mapper = new ObjectMapper();
ParsedTweets tweets = mapper.readValue(new File("tweet.json"), tweet.class);

example of json file

 {"text": "MVA/Transport. Greater Hume (Hume Hwy, Holbrook, NSW 2644) at 7 Mar 2017 03:58 #NSWRFS #MVATransport", "user": {"id": "4721717942", "name": "NSW Fire Updates"}, "lang": "en", "coordinates": { "coordinates": [147.273696, -35.785469] , "type":"Point"}, "created_at": "Mon Mar 06 17:29:31 +0000 2017"}
{"text": "Yes! Everything happens for a reason. The rain doesn't want the Swedru people to go& flood Westhills mall this evening", "user": {"id": "724288148", "name": "Pyper Pebbles"}, "lang": "en","created_at": "Mon Mar 06 17:19:49 +0000 2017"}
{"text": "5 Sure-Fire Ways That Can Help You Achieve Better Success In Life , "user": {"id": "41049329", "name": "StarCentral Magazine"}, "lang": "en","created_at": "Mon Mar 06 17:08:36 +0000 2017"}

tweet.class

public class Tweet {
private String text;
private String created_at;
private User user;
private Coordinates coordinates;

public Tweet(){

}

public String getText()
{
    return text;
}

public void setText(String text)
{
    this.text = text;
}

public String getCreated_at()
{
    return created_at;
}

public void setCreated_at(String created_at)
{
    this.created_at = created_at;
}

public User getUser()
{
    return user;
}

public void setUser(User user)
{
    this.user = user;
}

public Coordinates getCoordinates()
{
    return coordinates;
}

public void setCoordinates(Coordinates coordinates)
{
    this.coordinates = coordinates;
}

@Override
public String toString()
{
    return "ClassPojo [text = "+text+", created_at = "+created_at+", user = "+user+", coordinates = "+coordinates+"]";
}

}

Update ReadWriteJson implementation

public class ReadWriteJson {



 public static void main(String[] args) throws IOException {
        assert args != null & args.length > 0;
        List<Tweet> tweet = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        try (BufferedReader reader = new BufferedReader(new FileReader("tweets.json"))) {
            String line;
            while ((line = reader.readLine()) != null){
                tweet.add(mapper.readValue(line, Tweet.class));
            }
        }
    }
Kutam
  • 87
  • 2
  • 12

1 Answers1

0

Assuming the file contains tweets in the below format:

[{
    "id": "1",
    "text" : "abc"
}, {
    "id": "2",
    "text" : "def"
}]

And tweet class has the following fields:

class tweet {
    private String id;
    private String text;
    //getters and setters
    public String toString(){
        return this.id + "," + this.name + "," + this.text + "," + this.coordinates + "," + this.created_at;
}

You can use Jackson's TypeReference to deserialize the list, e.g.:

ObjectMapper mapper = new ObjectMapper();
List<tweet> tweets = mapper.readValue(new File("tweet.json"), new TypeReference<List<tweet>>(){});

Update As per the updated class and file structures, you need to read the file line by line (as it's not a valid json) and deserialize it into Tweet object, e.g.:

public class Test {
    public static void main(String[] args) throws Exception {
        List<Tweet> tweets = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        try(BufferedReader reader = new BufferedReader(new FileReader("tweet.json"))){
            String line;
            while((line = reader.readLine()) != null){
                tweets.add(mapper.readValue(line, Tweet.class));
            }
        }
    }
}
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Hey, sorry i thought it was posted but my internet was done. Can you check it again please. I just posted it – Kutam Apr 11 '17 at 13:50
  • yes thats what i was intending to do. read the json file line by line...thanks for the insight. But its not working there is an error message "Error:(28, 6) java: reached end of file while parsing". can you check the updated version i posted – Kutam Apr 11 '17 at 14:10
  • Error or exception? Could you post the stacktrace? – Darshan Mehta Apr 11 '17 at 14:14
  • its an error i think. there is an error with the parsing. the error shows "Error:(28, 6) java: reached end of file while parsing" which is the last line of the code. in " } " – Kutam Apr 11 '17 at 14:19
  • how do i use stacktrace? – Kutam Apr 11 '17 at 14:20
  • You might be missing starting/ending braces around the class, try the updated example in the answer (by creating a class named `Test` and copy/pasting the code). – Darshan Mehta Apr 11 '17 at 14:23
  • yes i forgot to put the last braces. There is still an error message saying ("exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "lang" (class Tweet), not marked as ignorable (4 known properties: "created_at", "user", "text", "coordinates"])) which is think is because the original json file have an object "lang" but i didnt put it in the tweet.class because i dont want to include that. so how do i tackle this? – Kutam Apr 11 '17 at 14:29
  • Annotate `Tweet` class with `@JsonIgnoreProperties(ignoreUnknown = true)` – Darshan Mehta Apr 11 '17 at 14:33
  • yes that work. Oh and one last thing how do i print the pojo(s)? the printing is not necessary. but i just want to see whats going on – Kutam Apr 11 '17 at 14:47
  • Override `toString()` ? – Darshan Mehta Apr 11 '17 at 15:05
  • i tried "System.out.print( tweet.add(mapper.readValue(line, Tweet.class)));" but the result is just true, true, true,.... | and what do you mean to use toString? – Kutam Apr 11 '17 at 15:22
  • http://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java – Darshan Mehta Apr 11 '17 at 15:28
  • I added the to string method in the test.class can you check? because its still not working – Kutam Apr 11 '17 at 15:40
  • Updated the answer again – Darshan Mehta Apr 11 '17 at 15:50
  • yes but its still an error, because it says cannot fine symbol for example symbol:variable id because in that class file there is only the class Test – Kutam Apr 11 '17 at 15:55
  • Sorry, you need to move it to `Tweet` class, I have updated the answer – Darshan Mehta Apr 11 '17 at 16:17
  • Hey can you help me further with my project please? – Kutam Apr 12 '17 at 17:10
  • Sure, what's the question? – Darshan Mehta Apr 12 '17 at 18:53
  • its regarding topic matching. so i want to analyse each tweet. and what topic they belong in. but with further conditins and specification. I sent you a message btw on facebook (im assuming thats you). If you care to look at the file its in google drive. – Kutam Apr 12 '17 at 19:27
  • I didn't get any message, you can however post a question here and ask for suggestions? – Darshan Mehta Apr 12 '17 at 19:50
  • It would be under message request as im not friends with you on facebook. Can you check again? And ok i will try to ask for suggestions here – Kutam Apr 12 '17 at 19:57
  • I only have a day left, would you be able to help me? If so, you can give me your email or gmail so I can share it with you on google drive – Kutam Apr 12 '17 at 22:09
  • I have included you in google drive. Now I have just over 24 hours to finish it :( Your help is greatly appreciated – Kutam Apr 13 '17 at 06:38