2016-01-25 3 views
1

Я пытаюсь разбить данные Json из файла file.my содержит массив Json, содержащий в нем 20 объектов. Я хочу, чтобы эти 20 объектов были в 20 разных файлах. Мой код работает и создает файлы & Я хочу сохранить их в другие объекты String тоже, данные в файлах jumbling.Confused, как разбить правильно!
моя проблема:Разделение 20 объектов Json-Array на отдельные массивы json-файлов?

1) Мой json formate в файле. 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"}] 
+0

Я не большой с JSON в файлах, но цикл через массив с для цикла, и в цикле, вы можете получить каждый объект. Например, 'JSONObject obj = input.getJSONObject (i);' Вот как вы их разделяете. Тогда вы должны записать это в файл. – bmarkham

+0

Спасибо! Я буду работать с вашим предложением. –

ответ

0

Согласно вашим снимкам экрана, все выглядит хорошо. Если вас беспокоит последовательность значений внутри объекта JSON, вам не нужно беспокоиться. См. Ссылку ниже для этого. Does the sequence of the values matter in a JSON object?

+0

Спасибо! но не можем ли мы их привести в порядок? –

+0

@DilipBobby: Не могли бы вы немного объяснить, почему заказ имеет важное значение? Поскольку вы можете позже извлекать объекты только по их ключу. –

+0

Просто, чтобы понять, что это такое. Сейчас я не беспокоюсь о последовательности. –

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); 
     } 
    } 
} 
+0

Спасибо! Я хочу, чтобы каждый объект находился в отдельном файле. например: array содержит 20 obj Мне нужны эти 20 из 20 отдельных файлов. Даже мой код выглядит сложным, он работает так же, как ваш! но я хочу, чтобы они были в порядке. –

+0

Разве мой код не делает то, что вы хотите? Что еще не работает? –

+0

получил небольшую ошибку, но я очистил это @Dzmitry Paulenka –

Смежные вопросы