【问题标题】:Splitting a 20 objects of Json-Array into spearte single array json files?将 20 个 Json-Array 对象拆分为单独的单个数组 json 文件?
【发布时间】:2016-01-25 06:21:33
【问题描述】:

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

1) 我在文件中的 json 格式。 我在拆分并写入文件后得到的 json 格式

我的代码

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,而是用for循环遍历数组,在循环中,你可以得到每个对象。比如JSONObject obj = input.getJSONObject(i);这就是你将它们分开的方式。然后你会把它写到一个文件中。
  • 谢谢!我会接受你的建议。

标签: java arrays json


【解决方案1】:

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

【讨论】:

  • 谢谢!但是我们不能把它们整理好吗?
  • @DilipBobby:您能否解释一下,为什么排序很重要?因为您以后只能通过它们的键来获取对象。
  • 只是想知道就是这样。现在我不担心顺序。
【解决方案2】:

您的代码似乎过于复杂...而且不清楚您要如何分隔对象? 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);
        }
    }
}

【讨论】:

  • 谢谢!我希望每个对象都在单独的文件中。例如:array 包含 20 个 obj 我需要 20 个单独的文件中的 20 个。即使我的代码看起来很复杂,它的工作原理和你的一样!但我希望它们按顺序排列。
  • 我的代码不是在做你想做的事吗?还有什么不工作?
  • 有小错误,但我清除了@Dzmitry Paulenka
  • :20个文件中只写了一个对象!我没有收到 20 个不同的文件。
  • 糟糕,我不小心使用了a.get(0) 而不是a.get(i)。编辑了一个答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 1970-01-01
  • 2017-05-20
  • 2017-07-31
  • 1970-01-01
  • 2015-04-12
相关资源
最近更新 更多