1

I have the following JSON file and wish to parse it to create tweet objects.

the tweet objects should contain user id, username, text, coordinates, and timestamp through the definition of POJO class.

{"text": "I think this would be good. Would hopefully light the fire in the later rounds if you knew you were losing", "user": {"id": "39455201", "name": "Elvis Sinosic"}, "lang": "en","created_at": "Mon Mar 06 22:48:07 +0000 2017"} {"text": "@Night_0f_Fire He's stuck in the alimony machine,, bust him out", "user": {"id": "2744192965", "name": "Mitch L"}, "lang": "en","created_at": "Mon Mar 06 22:47:53 +0000 2017"} 

this is what i did

import com.fasterxml.jackson.databind.*; import java.util.Arrays; import java.util.List; import java.util.Map; public class Tweets { private long userID; private String userName; private String text; private List<int> coordinates; private String timestamp; } ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper(); Tweets tweets = mapper.readValue(new("/Users/YusufNing/IdeaProjects/comp6700-2017/ass1/parsing/Tweets.java"), Tweets.class); 
5
  • Jackson is pretty straight forward. Create your .java classes with user id, username, text, coordinates, and timestamp. You can make them all string variables (makes it super easy to map). You can always parse out later once the data is mapped! Commented Apr 10, 2017 at 19:26
  • One piece of advice though, when you're doing the mapping DO NOT put it on the main thread. When you get the data from a server, you don't want Network Activity blocking the Main Thread, it'll cause poor performance. Commented Apr 10, 2017 at 19:29
  • im currently constructing ParsedTweets.java ill get back to you soon Commented Apr 10, 2017 at 19:29
  • the actual tweet is from a json file, so its not real-time Commented Apr 10, 2017 at 19:30
  • 1
    should be as easy as ParsedTweets tweet = mapper.readValue(new File("your_JSON_FILE.json"), ParsedTweets.class); Commented Apr 10, 2017 at 19:36

1 Answer 1

4

Jackson is pretty straight forward. Create your .java classes with user id, username, text, coordinates, and timestamp. You can make them all string variables (makes it super easy to map). You can always parse out later once the data is mapped

ParsedTweets.class:

public class ParsedTweets { private String id; private String username; private String text; private String coordinates; private String timestamp; } 

Implementation:

ObjectMapper mapper = new ObjectMapper(); String jsonTweeterString = "{'text': 'I think this would be good. Would hopefully light the fire in the later rounds if you knew you were losing', 'user': {'id': '39455201', 'name': 'Elvis Sinosic'}, 'lang': 'en','created_at': 'Mon Mar 06 22:48:07 +0000 2017'}"; ParsedTweets tweets = mapper.readValue(jsonTweeterString, ParsedTweets.class); 

To read from file you do:

ParsedTweets tweets = mapper.readValue(new File("/Users/Kutam/IdeaProjects/comp6700-2017/ass1/tweets.js‌​on"), ParsedTweets.class); 
Sign up to request clarification or add additional context in comments.

7 Comments

is the implementation also within the same parsedtweet.class and how to put this as an input /Users/Kutam/IdeaProjects/comp6700-2017/ass1/tweets.json
ParsedTweets tweets = mapper.readValue(new File("/Users/Kutam/IdeaProjects/comp6700-2017/ass1/tweets.json"), ParsedTweets.class);
i post the revised version on the original question, it still didnt work. can you check?
Error:(20, 1) java: class, interface, or enum expected Error:(21, 1) java: class, interface, or enum expected Error:(21, 123) java: <identifier> expected Error:(21, 125) java: reached end of file while parsing
Sounds like it's not finding your file or it can't find it. Put your tweets.json in the same path as your code, and see if you can put just tweets.json instead of the full path.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.