java - 将 Json-Array 的 20 个对象拆分为单独的单个数组 json 文件?

标签 java arrays json

我正在尝试从文件中拆分 Json 数据。我的文件包含一个 Json 数组,其中包含 20 个对象。我希望将这 20 个对象分成 20 个不同的文件。我的代码可以工作并创建文件,我也想将它们保存在不同的 String 对象中,文件中的数据很困惑。困惑如何以正确的方式拆分!
我的问题:

1) 文件中我的 json 格式。 enter image description here 分割并写入文件后我得到的 json 格式 enter image description here

我的代码

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 &amp; 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 &amp; 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"}]

最佳答案

根据您的屏幕截图,我认为一切都很好。如果您担心 JSON 对象内的值的顺序,那么您不必担心。请参阅以下链接以获取相同信息。 Does the sequence of the values matter in a JSON object?

关于java - 将 Json-Array 的 20 个对象拆分为单独的单个数组 json 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34986288/

相关文章:

java - 在java中通过线程显示图像序列

javascript - 添加重复的数组值

java | Gson |将 JSON 对象添加到现有的 JSON 文件

java - Android 线性布局上的 Weight 属性不起作用

java - 使用简单参数从单元测试调用主(spark)应用程序

java - 使用单个正则表达式验证每一行

java - 将数据存储在数组中并使用 for 循环下推项目

python - 如何使用 numpy 从同一数组中减去两列并将值放入它们自己的单列数组中?

php - 在 Android 中对 Json 输出进行排序

java - 位置 2 处出现意外字符 (") JSON 解析器 (Java)