Read Json file and Split the Tweet message

359 views Asked by At
    public class Tweets {

    private String Name;
    private String Username;
    private String Time;
    private long Followers;
    private long Following;
    private int Location;
    private String Tweet; 
    private double longitude; 
    private double latitude; 


    public Tweets(String name, String username, String time,
            long followers, long following, int location, String tweet,
            double longitude, double latitude) {

        Name = name;
        Username = username;
        Time = time;
        Followers = followers;
        Following = following;
        Location = location;
        Tweet = tweet;
        this.longitude = longitude;
        this.latitude = latitude;
    }
    public double getSentimentValue() {
        return sentimentValue;
    }
    public void setSentimentValue(double sentimentValue) {
        this.sentimentValue = sentimentValue;
    }
    public boolean isHaveisettheentimentvaluecalculateornot() {
        return haveisettheentimentvaluecalculateornot;
    }
    public void setHaveisettheentimentvaluecalculateornot(
            boolean haveisettheentimentvaluecalculateornot) {
        this.haveisettheentimentvaluecalculateornot = haveisettheentimentvaluecalculateornot;
    }
    public String getName() {
        return Name;
    }
    public String getUsername() {
        return Username;
    }
    public String getTime() {
        return Time;
    }
    public long getFollowers() {
        return Followers;
    }
    public long getFollowing() {
        return Following;
    }
    public int getLocation() {
        return Location;
    }
    public String getTweet() {
        return Tweet;
    }
    public double getLongitude() {
        return longitude;
    }
    public double getLatitude() {
        return latitude;
    }


    private double sentimentValue;
    private boolean haveisettheentimentvaluecalculateornot;





    @Override
    public String toString(){
        StringBuilder sb = new StringBuilder();
        sb.append("***** Employee Details *****\n");
        sb.append("Name="+getName()+"\n");
        sb.append("UserName="+getUsername()+"\n");
        sb.append("Time="+getTime()+"\n");
        sb.append("Followers="+getFollowers()+"\n");
        sb.append("Following="+getFollowing()+"\n");
        sb.append("Location="+getLocation());
        sb.append("\n*****************************");

        return sb.toString();
    }

Below the json file i have attached the code i used to open my file, it opens the file but i'm stuck with splitting the tweets in to single word.

private static final String filePath = "H:\\My Documents\\OO2\\Ass\\Tweets.Json";

    public static void main(String[] args) {

        try {
            // read the json file
            FileReader reader = new FileReader(filePath);

            JSONParser jsonParser = new JSONParser();
            JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

            // get a String from the JSON object
            String Username = (String) jsonObject.get("Username");
            System.out.println("The first name is: " + Username);

            // get a number from the JSON object
            long Name =  (long) jsonObject.get("Name");
            System.out.println("The accouunt name is: " + Name);

            // get an array from the JSON object
            JSONArray Tweet= (JSONArray) jsonObject.get("Tweet");

            // take the elements of the json array
            for(int i=0; i<Tweet.size(); i++){
                System.out.println("The " + i + " element of the array: "+Tweet.get(i));
            }
            Iterator i = Tweet.iterator();

            // take each value from the json array separately
            while (i.hasNext()) {
                JSONObject innerObj = (JSONObject) i.next();
                System.out.println("Tweet "+ innerObj.get("Tweet") + 
                        " with level " + innerObj.get("knowledge"));
            }
            // handle a structure into the json object
            JSONObject structure = (JSONObject) jsonObject.get("job");
            System.out.println("Into job structure, name: " + structure.get("name"));

        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParseException ex) {
            ex.printStackTrace();
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        }

    }

}I am trying to read this Json file and split the tweets message into single word and analysis tweets with sentiment.

{
    "User":"BarackObama",
    "UserName":"Barack Obama",
    "Tweet":"Happening now: President Obama is holding a press conference. Tune in. ofa.bo/s1QX",
    "Time":"Wed Nov 05 19:58:06 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"\"I'm going to squeeze every last bit of opportunity to make the world a better place over these last two years.\" ?President Obama",
    "Time":"Wed Nov 05 21:33:27 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"This is undeniable progress on the minimum wage. Congrats to the @OFA volunteers who are fighting to #RaiseTheWage. pic.twitter.com/m4cLUzYZQO",
    "Time":"Thu Nov 06 18:14:22 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"Jackie Robinson West All Stars: America's champions. pic.twitter.com/JAmZBdgNuB",
    "Time":"Thu Nov 06 23:19:11 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"The unemployment rate is falling as fast as any point in the last 30 years. Let's keep building that momentum. pic.twitter.com/IiTCcVJM7t",
    "Time":"Fri Nov 07 17:17:17 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  },
  {
    "User":"BarackObama",
    "User Name":"Barack Obama",
    "Tweet":"For the first time since the 1990s, the private sector has added at least 200K jobs for nine consecutive months. #ActOnJobs",
    "Time":"Fri Nov 07 19:28:31 +0000 2014",
    "Followers":49510304,
    "Following":646777,
    "Location":"Washington, DC",
    "FIELD8":""
  }
1

There are 1 answers

3
Ringo On

Something like returns only the Tweet text and stores it in a Tweet String, the only problem is that it will remove every ":" character in the tweet tho

String Tweet = "";
while ((sCurrentLine = bufferedReader.readLine()) != null) {
    if (sCurrentLine.contains("Tweet")){
        String[] parts = sCurrentLine.split(":");
        for (int i = 1; i < parts.length; i++) {
            Tweet = Tweet + parts[i];
        }
        break;
    }
}