我刚接触到这一点,我的文件包含50.000条带有JSON格式的推文,我需要转换或使其可读。我试着使用这段代码但是如何让它写入文件呢?
package analysis;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.util.Scanner;
import com.google.gson.Gson;
public class p {
public static void performAnalysis() {
File tweets;
String[] categories;
tweets = new File("/home/user/Desktop/tweets.txt");
categories = tweets.list();
for (int i = 0; i < categories.length; ++i) {
String category = categories[i];
File file = new File(tweets, category);
try {
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
String rawTweet = sc.nextLine().trim();
if (rawTweet.equals(""))
continue;
Tweet parsedTweet = new Gson().fromJson(rawTweet, Tweet.class);
//System.out.println(parsedTweet.getBody());
}
sc.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
另一个班级是
package analysis;
import java.io.Serializable;
public class Tweet implements Serializable{
private long ID;
private String body;
public Tweet(long tweetID, String body)
{
this.body = body;
ID = tweetID;
}
public Tweet(long tweetId)
{
ID = tweetId;
}
public String getBody()
{
return body;
}
public void setBody(String body){
this.body = body;
}
public long getID()
{
return ID;
}
public void setId(long tweetId)
{
ID = tweetId;
}
}
包含的文件 推文喜欢
{"filter_level":"low","retweeted":false,"in_reply_to_screen_name":null,"possib
ly_sensitive":false,"truncated":false,"lang":"en","in_reply_to_status_id_str": null,"id":712044789663924224,"extended_entities":{"media":[{"sizes":{"thumb":{"w":150,"resize":"crop","h":150},"small": {"w":340,"resize":"fit","h":227},"medium":{"w":600,"resize":"fit","h":400},"large":{"w":1024,"resize":"fit","h":683}},"source_user_id":38142624,"media_url":"http://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg"","type":"photo",,"id":710442467104055296,"media_url_https":"https://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg","expanded_url":"http://twitter.com/standardsport/status/710442468760748032/photo/1","source_user_id_str":"38142624","indices":[120,140],"source_status_id_str":"710442468760748032","source_status_id":710442468760748032,"id_str":"710442467104055296"},{"sizes":{"small":{"w":340,"resize":"fit","h":227},"thumb":{"w":150,"resize":"crop","h":150},"medium":{"w":600,"resize":"fit","h":400},"large":{"w":1024,"resize":"fit","h":683}},"source_user_id":38142624,"media_url":"http://pbs.twimg.com/media/Cdv_qOpW4AEpErk.jpg","id":710442467192135681,"media_url_https":"https://pbs.twimg.com/media/Cdv_qOpW4AEpErk.jpg","expanded_url":"http://twitter.com/standardsport/status/710442468760748032/photo/1","source_user_id_str":"38142624","indices":[120,140],"source_status_id_str":"710442468760748032","source_status_id":710442468760748032,"id_str":"710442467192135681"}]},"in_reply_to_user_id_str":null,"timestamp_ms":"1458599681665","in_reply_to_status_id":null,"created_at":"Mon Mar 21 22:34:41 +0000
第二部分
2016","favorite_count":0,"place":null,"coordinates":null,"text":"RT @WHUFC_News: West Ham are the only team in the top half of the Premier League without a player in the England squad. ,"contributors":null,"retweeted_status":{"filter_level":"low","retweeted":false,"in_reply_to_screen_name":null,"possibly_sensitive":false,"truncated":false,"lang":"en","in_reply_to_status_id_str":null,"id":712035466573422594,"extended_entities":{"media":[{"sizes":{"thumb":{"w":150,"resize":"crop","h":150},"small":{"w":340,"resize":"fit","h":227},"medium":{"w":600,"resize":"fit","h":400},"large":{"w":1024,"resize":"fit","h":683}},"source_user_id":38142624,"media_url":"http://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg""id":710442467104055296,"media_url_https":"https://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg","expanded_url":"http://twitter.com/standardsport/status/710442468760748032/photo/1","source_user_id_str":"38142624","indices":[104,127],"source_status_id_str":"710442468760748032","source_status_id":710442468760748032,"id_str":"710442467104055296"},{"sizes":{"small":{"w":340,"resize":"fit","h":227},"thumb":{"w":150,"resize":"crop","h":150},"medium":{"w":600,"resize":"fit","h":400},"large":{"w":1024,"resize":"fit","h":683}},"source_user_id":38142624,"media_url":"http://pbs.twimg.com/media/Cdv_qOpW4AEpErk.jpg","display_url":"pic.twitter.com/OFpmAARFZR","type":"photo","id":710442467192135681,"media_url_https":"https://pbs.twimg.com/media/Cdv_qOpW4AEpErk.jpg","expanded_url":"http://twitter.com/standardsport/status/710442468760748032/photo/1","source_user_id_str":"38142624","indices":[104,127],"source_status_id_str":"710442468760748032","source_status_id":710442468760748032,"id_str":"710442467192135681"}]},"in_reply_to_user_id_str":null,"in_reply_to_status_id":null,"created_at":"Mon Mar 21 21:57:38 +0000 2016","favorite_count":51,"place":null,"coordinates":null,"text":"West Ham are the only team in the top half of the Premier League without a player in the England squad.,"contributors":null,"geo":null,"entities":{"symbols":[],"urls":[],"hashtags":[],"media":[{"sizes":{"thumb":{"w":150,"resize":"crop","h":150},"small":{"w":340,"resize":"fit","h":227},"medium":{"w":600,"resize":"fit","h":400},"large":{"w":1024,"resize":"fit","h":683}},"source_user_id":38142624,"media_url":"http://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg","type":"photo","id":710442467104055296,,,"source_user_id_str":"38142624","indices":[104,127],"source_status_id_str":"710442468760748032","source_status_id":710442468760748032,"id_str":"710442467104055296"}],"user_mentions":[]},"is_quote_status":false,"source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for
第三部分
iPhone<\/a>","favorited":false,"in_reply_to_user_id":null,"retweet_count":47,"
id_str":"712035466573422594","user":{"location":"East London","default_profile":false,"statuses_count":87630,"profile_background_tile":false,"lang":"en","profile_link_color":"CC3366","profile_banner_url":"https://pbs.twimg.com/profile_banners/346499404/1457051522","id":346499404,"following":null,"favourites_count":2879,"protected":false,"profile_text_color":"333333","verified":false,"description":"Largest independent West Ham account. News, Transfer Rumours, Images, Stats, Match Updates and more. Est. August '11.","contributors_enabled":false,"profile_sidebar_border_color":"DBE9ED","name":"West Ham News","profile_background_color":"DBE9ED","created_at":"Mon Aug 01 11:00:44 +0000 2011","default_profile_image":false,"followers_count":44318,"profile_image_url_https":"https://pbs.twimg.com/profile_images/705551398650388481/_o-dfUjR_normal.jpg","geo_enabled":false,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme17/bg.gif","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme17/bg.gif","follow_request_sent":null,"url":"http://www.westham-news.com","utc_offset":null,"time_zone":null,"notifications":null,"profile_use_background_image":true,"friends_count":508,"profile_sidebar_fill_color":"E6F6F9","screen_name":"WHUFC_News","id_str":"346499404","profile_image_url":"http://pbs.twimg.com/profile_images/705551398650388481/_o-dfUjR_normal.jpg","listed_count":240,"is_translator":false}},"geo":null,"entities":{"symbols":[],"urls":[],"hashtags":[],"media":[{"sizes":{"thumb":{"w":150,"resize":"crop","h":150},"small":{"w":340,"resize":"fit","h":227},"medium":{"w":600,"resize":"fit","h":400},"large":{"w":1024,"resize":"fit","h":683}},"source_user_id":38142624,"media_url":"http://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg","id":710442467104055296,"media_url_https":"https://pbs.twimg.com/media/Cdv_qOUW4AAfExX.jpg","expanded_url":"http://twitter.com/standardsport/status/710442468760748032/photo/1","source_user_id_str":"38142624","indices":[120,140],"source_status_id_str":"710442468760748032","source_status_id":710442468760748032,"id_str":"710442467104055296"}],"user_mentions":[{"id":346499404,"name":"West Ham News","indices":[3,14],"screen_name":"WHUFC_News","id_str":"346499404"}]},"is_quote_status":false,"source":"<a href=\"http://twitter.com/download/iphone\" rel=\"nofollow\">Twitter for iPhone<\/a>","favorited":false,"in_reply_to_user_id":null,"retweet_count":0,"id_str":"712044789663924224","user":{"location":null,"default_profile":true,"statuses_count":16826,"profile_background_tile":false,"lang":"en","profile_link_color":"0084B4","profile_banner_url":"https://pbs.twimg.com/profile_banners/559823246/1355067792","id":559823246,"following":null,"favourites_count":84,"protected":false,"profile_text_color":"333333","verified":false,"description":null,"contributors_enabled":false,"profile_sidebar_border_color":"C0DEED","name":"John mcginty","profile_background_color":"C0DEED","created_at":"Sat Apr 21 19:44:37 +0000 2012","default_profile_image":false,"followers_count":123,"profile_image_url_https":"https://pbs.twimg.com/profile_images/690829546032074752/8qjDgLXe_normal.jpg","geo_enabled":true,"profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","follow_request_sent":null,"url":null,"utc_offset":0,"time_zone":"London","notifications":null,"profile_use_background_image":true,"friends_count":216,"profile_sidebar_fill_color":"DDEEF6","screen_name":"jmmc88","id_str":"559823246","profile_image_url":"http://pbs.twimg.com/profile_images/690829546032074752/8qjDgLXe_normal.jpg","listed_count":8,"is_translator":false}}
答案 0 :(得分:1)
如果这个问题真的是关于格式化查询JSON文件的问题,那么我就跳过编程部分,只使用任何JSON浏览工具。例如。尝试在Firefox Developer Edition中打开该文件。
另一种选择是使用jq,并将输出重定向到一个文件(假设你使用一些类似UNIX / UNIX的shell):
cat tweets.json | jq . > output.json
但这将是一个大文件。您可以使用ja的查询语法制作一些较小的文件。