Search code examples
javajsontwitter

Parser implementation using Jackson


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));
            }
        }
    }

Solution

  • 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));
                }
            }
        }
    }