我正在尝试从file.my文件中拆分Json数据,其中包含一个包含20个对象的Json数组。我想将这20个对象分成20个不同的文件。我的代码可以工作并创建文件&我想将它们保存在不同的String对象中,文件中的数据是jumbling.Confused如何以正确的方式分割!
我的问题:
1)我的json合成文件。 json formate我在分割并写入文件
之后得到了我的代码
import java.io.*;
import java.util.*;
import org.json.*;
public class JsonSpilt {
private static final int BATCH_SIZE = 10;
public static void flushFile(List<Object> objects, int d) throws Exception {
try (FileOutputStream output = new FileOutputStream("/home/system5/Music/tweet-" + d
+ ".json");
Writer writer = new OutputStreamWriter(output, "UTF-8")) {
JSONArray jsonArray = new JSONArray(objects);
jsonArray.write(writer);
}
}
public static void main(String[] args) throws Exception {
int outputIndex = 0;
try (InputStream input = new BufferedInputStream(
new FileInputStream("/home/system5/Videos/tweet.json"))) {
JSONTokener tokener = new JSONTokener(input);
if (tokener.nextClean() != '[') {
throw tokener.syntaxError("Expected start of JSON array");
}
List<Object> jsonObjects = new ArrayList<>();
while (tokener.nextClean() != ']') {
tokener.back();
jsonObjects.add(tokener.nextValue());
if (jsonObjects.size() == BATCH_SIZE) {
flushFile(jsonObjects, outputIndex);
jsonObjects.clear();
outputIndex++;
}
if (tokener.nextClean() != ',') {
tokener.back();
}
}
if (!jsonObjects.isEmpty()) {
flushFile(jsonObjects, outputIndex);
}
// Verify that end of input is reached.
if (tokener.nextClean() != 0) {
throw tokener.syntaxError("Expected end of document");
}
}
}}
示例输入包含json数组中的两个对象。
[{"created_at":"Thu Jan 07 11:14:54 +0000 2016","id":685057015547220000,"id_str":"685057015547219968","text":"RT @makeinindia: Calling all techies! Collaborate, brainstorm & solve urban design problems at Hackathon 2016 #MakeInIndia Week https://t.c…","source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":3479265073,"id_str":"3479265073","name":"dilip bobby","screen_name":"heymailme143","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":10,"friends_count":35,"listed_count":3,"created_at":"Mon Sep 07 07:11:18 +0000 2015","favourites_count":1,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":309,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https":"https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Dec 20 12:47:16 +0000 2015","id":678557275829592000,"id_str":"678557275829592064","text":"Calling all techies! Collaborate, brainstorm & solve urban design problems at Hackathon 2016 #MakeInIndia Week https://t.co/ojP9Ih4p2Z","source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":2785480981,"id_str":"2785480981","name":"Make in India","screen_name":"makeinindia","location":"","description":"To register for Make in India Week, click here https://t.co/unsIaezqK6","url":"http://t.co/ZBCKavarN4","entities":{"url":{"urls":[{"url":"http://t.co/ZBCKavarN4","expanded_url":"http://www.makeinindia.com","display_url":"makeinindia.com","indices":[0,22]}]},"description":{"urls":[{"url":"https://t.co/unsIaezqK6","expanded_url":"http://www.makeinindia.com/mumbai-week","display_url":"makeinindia.com/mumbai-week","indices":[47,70]}]}},"protected":false,"followers_count":866474,"friends_count":35,"listed_count":924,"created_at":"Tue Sep 02 07:31:24 +0000 2014","favourites_count":16,"utc_offset":19800,"time_zone":"New Delhi","geo_enabled":true,"verified":true,"statuses_count":4869,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/514373412588765184/jQwP6ERL_normal.jpeg","profile_image_url_https":"https://pbs.twimg.com/profile_images/514373412588765184/jQwP6ERL_normal.jpeg","profile_banner_url":"https://pbs.twimg.com/profile_banners/2785480981/1450423946","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":false,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":1624,"favorite_count":3168,"entities":{"hashtags":[{"text":"MakeInIndia","indices":[97,109]}],"symbols":[],"user_mentions":[],"urls":[],"media":[{"id":678556556489658400,"id_str":"678556556489658369","indices":[115,138],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"photo","sizes":{"large":{"w":800,"h":800,"resize":"fit"},"small":{"w":340,"h":340,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":600,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":678556556489658400,"id_str":"678556556489658369","indices":[115,138],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"animated_gif","sizes":{"large":{"w":800,"h":800,"resize":"fit"},"small":{"w":340,"h":340,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":600,"resize":"fit"}},"video_info":{"aspect_ratio":[1,1],"variants":[{"bitrate":0,"content_type":"video/mp4","url":"https://pbs.twimg.com/tweet_video/CWq3l82UEAEjnrV.mp4"}]}}]},"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en"},"is_quote_status":false,"retweet_count":1624,"favorite_count":0,"entities":{"hashtags":[{"text":"MakeInIndia","indices":[114,126]}],"symbols":[],"user_mentions":[{"screen_name":"makeinindia","name":"Make in India","id":2785480981,"id_str":"2785480981","indices":[3,15]}],"urls":[],"media":[{"id":678556556489658400,"id_str":"678556556489658369","indices":[143,144],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"photo","sizes":{"large":{"w":800,"h":800,"resize":"fit"},"small":{"w":340,"h":340,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":600,"resize":"fit"}},"source_status_id":678557275829592000,"source_status_id_str":"678557275829592064","source_user_id":2785480981,"source_user_id_str":"2785480981"}]},"extended_entities":{"media":[{"id":678556556489658400,"id_str":"678556556489658369","indices":[143,144],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"animated_gif","sizes":{"large":{"w":800,"h":800,"resize":"fit"},"small":{"w":340,"h":340,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"medium":{"w":600,"h":600,"resize":"fit"}},"source_status_id":678557275829592000,"source_status_id_str":"678557275829592064","source_user_id":2785480981,"source_user_id_str":"2785480981","video_info":{"aspect_ratio":[1,1],"variants":[{"bitrate":0,"content_type":"video/mp4","url":"https://pbs.twimg.com/tweet_video/CWq3l82UEAEjnrV.mp4"}]}}]},"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en"},{"created_at":"Thu Jan 07 11:14:23 +0000 2016","id":685056883325972500,"id_str":"685056883325972480","text":"RT @IntelIndia: Grammy winning composer @arrahman uses Intel Curie to turn fingertips into guitars at #CES https://t.co/5NJ3nWofyi https://…","source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":3479265073,"id_str":"3479265073","name":"dilip bobby","screen_name":"heymailme143","location":"","description":"","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":10,"friends_count":35,"listed_count":3,"created_at":"Mon Sep 07 07:11:18 +0000 2015","favourites_count":1,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":309,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https":"https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Wed Jan 06 11:23:57 +0000 2016","id":684696902030475300,"id_str":"684696902030475265","text":"Grammy winning composer @arrahman uses Intel Curie to turn fingertips into guitars at #CES https://t.co/5NJ3nWofyi https://t.co/HWyeANow85","source":"<a href=\"http://www.sprinklr.com\" rel=\"nofollow\">Sprinklr</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":45800443,"id_str":"45800443","name":"Intel India","screen_name":"IntelIndia","location":"India","description":"Intel India’s official Twitter handle – For technologies and devices that enrich people's lives; House Rules: https://t.co/k0OmxmHmeO","url":"http://t.co/BnWL5Xl2Kp","entities":{"url":{"urls":[{"url":"http://t.co/BnWL5Xl2Kp","expanded_url":"http://www.intel.in","display_url":"intel.in","indices":[0,22]}]},"description":{"urls":[{"url":"https://t.co/k0OmxmHmeO","expanded_url":"https://tr.im/IoBoX","display_url":"tr.im/IoBoX","indices":[112,135]}]}},"protected":false,"followers_count":77465,"friends_count":549,"listed_count":311,"created_at":"Tue Jun 09 09:13:21 +0000 2009","favourites_count":3321,"utc_offset":19800,"time_zone":"New Delhi","geo_enabled":true,"verified":true,"statuses_count":12901,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http://pbs.twimg.com/profile_background_images/378800000172020428/8hBrBebn.jpeg","profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/378800000172020428/8hBrBebn.jpeg","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/685423865598619652/Cqe40-1O_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/685423865598619652/Cqe40-1O_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/45800443/1438925471","profile_link_color":"0070C5","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"86C6E3","profile_text_color":"444444","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":1822,"favorite_count":2653,"entities":{"hashtags":[{"text":"CES","indices":[86,90]}],"symbols":[],"user_mentions":[{"screen_name":"arrahman","name":"A.R.Rahman","id":19895282,"id_str":"19895282","indices":[24,33]}],"urls":[{"url":"https://t.co/5NJ3nWofyi","expanded_url":"http://www.intel.sg/ces","display_url":"intel.sg/ces","indices":[91,114]}],"media":[{"id":684696901934006300,"id_str":"684696901934006272","indices":[115,138],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":{"medium":{"w":599,"h":337,"resize":"fit"},"small":{"w":340,"h":191,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":599,"h":337,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":684696901934006300,"id_str":"684696901934006272","indices":[115,138],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":{"medium":{"w":599,"h":337,"resize":"fit"},"small":{"w":340,"h":191,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":599,"h":337,"resize":"fit"}}}]},"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en"},"is_quote_status":false,"retweet_count":1822,"favorite_count":0,"entities":{"hashtags":[{"text":"CES","indices":[102,106]}],"symbols":[],"user_mentions":[{"screen_name":"IntelIndia","name":"Intel India","id":45800443,"id_str":"45800443","indices":[3,14]},{"screen_name":"arrahman","name":"A.R.Rahman","id":19895282,"id_str":"19895282","indices":[40,49]}],"urls":[{"url":"https://t.co/5NJ3nWofyi","expanded_url":"http://www.intel.sg/ces","display_url":"intel.sg/ces","indices":[107,130]}],"media":[{"id":684696901934006300,"id_str":"684696901934006272","indices":[139,140],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":{"medium":{"w":599,"h":337,"resize":"fit"},"small":{"w":340,"h":191,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":599,"h":337,"resize":"fit"}},"source_status_id":684696902030475300,"source_status_id_str":"684696902030475265","source_user_id":45800443,"source_user_id_str":"45800443"}]},"extended_entities":{"media":[{"id":684696901934006300,"id_str":"684696901934006272","indices":[139,140],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":{"medium":{"w":599,"h":337,"resize":"fit"},"small":{"w":340,"h":191,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":599,"h":337,"resize":"fit"}},"source_status_id":684696902030475300,"source_status_id_str":"684696902030475265","source_user_id":45800443,"source_user_id_str":"45800443"}]},"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en"}]
答案 0 :(得分:0)
根据你的截图,一切看起来都很好。如果您担心JSON对象中的值序列,那么您不必担心。请参阅以下链接。 Does the sequence of the values matter in a JSON object?
答案 1 :(得分:0)
你的代码看起来过于复杂......而且你不清楚你想如何分离对象? 20到20个文件..每个文件是一个对象吗?你可以比这更容易做到:
public class Main {
public static void main(String[] args) throws Exception {
String filePath = "tweets.json";
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"))) {
JSONTokener tokener = new JSONTokener(reader);
JSONArray a = new JSONArray(tokener);
for (int i = 0; i < a.length(); ++i) {
flushFile((JSONObject) a.get(i), i);
}
}
}
public static void flushFile(JSONObject obj, int d) throws Exception {
try (FileOutputStream output = new FileOutputStream("/home/dzmitry/downloads/so-test/tweet-" + d + ".json");
Writer writer = new OutputStreamWriter(output, "UTF-8")) {
obj.write(writer);
}
}
}