如何解析这个JSONObject,从twitter搜索API中获取数组内部的值,以获得每个tweet(名称、正文)



这是我使用search/tweets.api时得到的响应。

{"statuses":[{"created_at":"Sun Jul 29 01:30:52 +0000 2018","id":1023380303648354304,"id_str":"1023380303648354304","text":"RT @FCFSeleccionCol: #FCFSub21 nnSemifinal nnEn marcha el juego en el Romelio Martínez!nnColombia 0 - Haití 0. ⚽⚽. #ColombiaJuegaEnCasa","truncated":false,"entities":{"hashtags":[{"text":"FCFSub21","indices":[21,30]},{"text":"ColombiaJuegaEnCasa","indices":[115,135]}],"symbols":[],"user_mentions":[{"screen_name":"FCFSeleccionCol","name":"Selección Colombia","id":1117317140,"id_str":"1117317140","indices":[3,19]}],"urls":[]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>","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":905605889859743745,"id_str":"905605889859743745","name":"Jesus David💯","screen_name":"ReyDavid420","location":"Barranquilla, Colombia","description":"Barranquillero, actor, compositor, rapero y futbolista.","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":2,"friends_count":67,"listed_count":0,"created_at":"Thu Sep 07 01:37:24 +0000 2017","favourites_count":593,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":770,"lang":"es","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/1010626181044072449/oRCkMA1g_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/1010626181044072449/oRCkMA1g_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/905605889859743745/1504748847","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Jul 29 00:06:15 +0000 2018","id":1023359010630758400,"id_str":"1023359010630758400","text":"#FCFSub21 nnSemifinal nnEn marcha el juego en el Romelio Martínez!nnColombia 0 - Haití 0. ⚽⚽. #ColombiaJuegaEnCasa","truncated":false,"entities":{"hashtags":[{"text":"FCFSub21","indices":[0,9]},{"text":"ColombiaJuegaEnCasa","indices":[94,114]}],"symbols":[],"user_mentions":[],"urls":[]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>","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":1117317140,"id_str":"1117317140","name":"Selección Colombia","screen_name":"FCFSeleccionCol","location":"","description":"Cuenta Oficial Selecciones Colombia de Fútbol / Federación Colombiana de Fútbol. ⚽️🇨🇴","url":"https://t.co/bdS4TJcR5h","entities":{"url":{"urls":[{"url":"https://t.co/bdS4TJcR5h","expanded_url":"http://www.fcf.com.co/","display_url":"fcf.com.co","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":4572748,"friends_count":54,"listed_count":3734,"created_at":"Thu Jan 24 17:29:53 +0000 2013","favourites_count":37,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":true,"statuses_count":15083,"lang":"es","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5EC3D","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/1017800505978957824/1d1KAGzZ_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/1017800505978957824/1d1KAGzZ_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/1117317140/1531348783","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":13,"favorite_count":71,"favorited":false,"retweeted":false,"lang":"es"},"is_quote_status":false,"retweet_count":13,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"es"},{"created_at":"Sun Jul 29 01:30:50 +0000 2018","id":1023380295473684481,"id_str":"1023380295473684481","text":"RT @radafil_diaz: Señores Haiti le gana a Colombian en los Juegos Centro Americano y del Caribe, bien por nuestro vecino mientra @fedofutbo…","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"radafil_diaz","name":"Radafil Diaz","id":816700741339516930,"id_str":"816700741339516930","indices":[3,16]}],"urls":[]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>","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":706612947,"id_str":"706612947","name":"Miguel Gil","screen_name":"mgil047","location":"La Vega, Dominican Republic","description":"Lic. Adm de empresas, Tec. Informático, amante del fútbol, cronista transmisión del atlético vega real, nsiempre se puede mejorar","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":342,"friends_count":556,"listed_count":5,"created_at":"Wed Oct 09 16:31:19 +0000 2013","favourites_count":1201,"utc_offset":null,"time_zone":null,"geo_enabled":true,"verified":false,"statuses_count":3015,"lang":"es","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://pbs.twimg.com/profile_images/998583089893818369/Ape7HNbM_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/998583089893818369/Ape7HNbM_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/706612947/1425593955","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Jul 29 01:29:27 +0000 2018","id":1023379945064681472,"id_str":"1023379945064681472","text":"Señores Haiti le gana a Colombian en los Juegos Centro Americano y del Caribe, bien por nuestro vecino mientra… https://t.co/p7Z4xNKEIO","truncated":true,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[{"url":"https://t.co/p7Z4xNKEIO","expanded_url":"https://twitter.com/i/web/status/1023379945064681472","display_url":"twitter.com/i/web/status/1…","indices":[112,135]}]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>","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":816700741339516930,"id_str":"816700741339516930","name":"Radafil Diaz","screen_name":"radafil_diaz","location":"Vivo en Panama, Dominicano de ","description":"Amante del deportes dominicano, Beseball,Basqueball, y mi pacion por el futbol, no tiene limite, Amante enamorado Del Barcelona FC, y Atleticos Vega Real!!","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":374,"friends_count":635,"listed_count":3,"created_at":"Wed Jan 04 17:40:05 +0000 2017","favourites_count":86379,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":27444,"lang":"es","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/1015056769138257923/YIMDdGlX_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/1015056769138257923/YIMDdGlX_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/816700741339516930/1531107483","profile_link_color":"1DA1F2","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":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":1,"favorite_count":1,"favorited":false,"retweeted":false,"lang":"es"},"is_quote_status":false,"retweet_count":1,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"es"}],"search_metadata":{"completed_in":0.042,"max_id":1023380303648354304,"max_id_str":"1023380303648354304","next_results":"?max_id=1023380295473684480&q=Haiti&count=2&include_entities=1","query":"Haiti","refresh_url":"?since_id=1023380303648354304&q=Haiti&include_entities=1","count":2,"since_id":0,"since_id_str":"0"}}

我无法解析它来获得状态为(created_at,name,text等等(的值

我尝试过这种方法,但不起作用

public void addItems(JSONArray response){
for (int i = 0; i < response.length(); i++ ) {
//convert each object to a Tweet model
//add that twiit model to our data source
//notify the adapter that we've added an item
try {
Tweet tweet = Tweet.fromJSON(response.getJSONObject(i));
tweets.add(tweet);
tweetAdapter.notifyItemInserted(tweets.size() - 1);
} catch (JSONException e){
e.printStackTrace();
}
}
}

这是来自JSON方法:

//deserialize the JSON
public static Tweet fromJSON(JSONObject jsonObject) throws JSONException{
Tweet tweet = new Tweet();
//extract the values from JSON
tweet.body = jsonObject.getString("text");
tweet.uid = jsonObject.getLong("id");
tweet.createdAt = jsonObject.getString("created_at");
tweet.user = User.fromJSON(jsonObject.getJSONObject("user"));
//tweet.extendedEntities = ExtendedEntities.fromJSON
return tweet;

查看此示例如何解析json&amp;获取数据。

String str = "{"statuses":[{"created_at":"Sun Jul 29 01:30:52 +0000 2018","id":1023380303648354304,"id_str":"1023380303648354304","text":"RT @FCFSeleccionCol: #FCFSub21 \n\nSemifinal \n\nEn marcha el juego en el Romelio Martínez!\n\nColombia 0 - Haití 0. ⚽⚽. #ColombiaJuegaEnCasa","truncated":false,"entities":{"hashtags":[{"text":"FCFSub21","indices":[21,30]},{"text":"ColombiaJuegaEnCasa","indices":[115,135]}],"symbols":[],"user_mentions":[{"screen_name":"FCFSeleccionCol","name":"Selección Colombia","id":1117317140,"id_str":"1117317140","indices":[3,19]}],"urls":[]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\">Twitter for Android<\/a>","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":905605889859743745,"id_str":"905605889859743745","name":"Jesus DaviduD83DuDCAF","screen_name":"ReyDavid420","location":"Barranquilla, Colombia","description":"Barranquillero, actor, compositor, rapero y futbolista.","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":2,"friends_count":67,"listed_count":0,"created_at":"Thu Sep 07 01:37:24 +0000 2017","favourites_count":593,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":770,"lang":"es","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1010626181044072449\/oRCkMA1g_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1010626181044072449\/oRCkMA1g_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/905605889859743745\/1504748847","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Jul 29 00:06:15 +0000 2018","id":1023359010630758400,"id_str":"1023359010630758400","text":"#FCFSub21 \n\nSemifinal \n\nEn marcha el juego en el Romelio Martínez!\n\nColombia 0 - Haití 0. ⚽⚽. #ColombiaJuegaEnCasa","truncated":false,"entities":{"hashtags":[{"text":"FCFSub21","indices":[0,9]},{"text":"ColombiaJuegaEnCasa","indices":[94,114]}],"symbols":[],"user_mentions":[],"urls":[]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href=\"http:\/\/twitter.com\" rel=\"nofollow\">Twitter Web Client<\/a>","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":1117317140,"id_str":"1117317140","name":"Selección Colombia","screen_name":"FCFSeleccionCol","location":"","description":"Cuenta Oficial Selecciones Colombia de Fútbol \/ Federación Colombiana de Fútbol. ⚽️uD83CuDDE8uD83CuDDF4","url":"https:\/\/t.co\/bdS4TJcR5h","entities":{"url":{"urls":[{"url":"https:\/\/t.co\/bdS4TJcR5h","expanded_url":"http:\/\/www.fcf.com.co\/","display_url":"fcf.com.co","indices":[0,23]}]},"description":{"urls":[]}},"protected":false,"followers_count":4572748,"friends_count":54,"listed_count":3734,"created_at":"Thu Jan 24 17:29:53 +0000 2013","favourites_count":37,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":true,"statuses_count":15083,"lang":"es","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5EC3D","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\/1017800505978957824\/1d1KAGzZ_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1017800505978957824\/1d1KAGzZ_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1117317140\/1531348783","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":13,"favorite_count":71,"favorited":false,"retweeted":false,"lang":"es"},"is_quote_status":false,"retweet_count":13,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"es"},{"created_at":"Sun Jul 29 01:30:50 +0000 2018","id":1023380295473684481,"id_str":"1023380295473684481","text":"RT @radafil_diaz: Señores Haiti le gana a Colombian en los Juegos Centro Americano y del Caribe, bien por nuestro vecino mientra @fedofutbo…","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions":[{"screen_name":"radafil_diaz","name":"Radafil Diaz","id":816700741339516930,"id_str":"816700741339516930","indices":[3,16]}],"urls":[]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\">Twitter for Android<\/a>","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":706612947,"id_str":"706612947","name":"Miguel Gil","screen_name":"mgil047","location":"La Vega, Dominican Republic","description":"Lic. Adm de empresas, Tec. Informático, amante del fútbol, cronista transmisión del atlético vega real, \nsiempre se puede mejorar","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":342,"friends_count":556,"listed_count":5,"created_at":"Wed Oct 09 16:31:19 +0000 2013","favourites_count":1201,"utc_offset":null,"time_zone":null,"geo_enabled":true,"verified":false,"statuses_count":3015,"lang":"es","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:\/\/pbs.twimg.com\/profile_images\/998583089893818369\/Ape7HNbM_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/998583089893818369\/Ape7HNbM_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/706612947\/1425593955","profile_link_color":"1DA1F2","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Jul 29 01:29:27 +0000 2018","id":1023379945064681472,"id_str":"1023379945064681472","text":"Señores Haiti le gana a Colombian en los Juegos Centro Americano y del Caribe, bien por nuestro vecino mientra… https:\/\/t.co\/p7Z4xNKEIO","truncated":true,"entities":{"hashtags":[],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/t.co\/p7Z4xNKEIO","expanded_url":"https:\/\/twitter.com\/i\/web\/status\/1023379945064681472","display_url":"twitter.com\/i\/web\/status\/1…","indices":[112,135]}]},"metadata":{"iso_language_code":"es","result_type":"recent"},"source":"<a href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\">Twitter for Android<\/a>","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":816700741339516930,"id_str":"816700741339516930","name":"Radafil Diaz","screen_name":"radafil_diaz","location":"Vivo en Panama, Dominicano de ","description":"Amante del deportes dominicano, Beseball,Basqueball, y mi pacion por el futbol, no tiene limite, Amante enamorado Del Barcelona FC, y Atleticos Vega Real!!","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":374,"friends_count":635,"listed_count":3,"created_at":"Wed Jan 04 17:40:05 +0000 2017","favourites_count":86379,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":27444,"lang":"es","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"F5F8FA","profile_background_image_url":null,"profile_background_image_url_https":null,"profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1015056769138257923\/YIMDdGlX_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1015056769138257923\/YIMDdGlX_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/816700741339516930\/1531107483","profile_link_color":"1DA1F2","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":false,"following":false,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":1,"favorite_count":1,"favorited":false,"retweeted":false,"lang":"es"},"is_quote_status":false,"retweet_count":1,"favorite_count":0,"favorited":false,"retweeted":false,"lang":"es"}],"search_metadata":{"completed_in":0.042,"max_id":1023380303648354304,"max_id_str":"1023380303648354304","next_results":"?max_id=1023380295473684480&q=Haiti&count=2&include_entities=1","query":"Haiti","refresh_url":"?since_id=1023380303648354304&q=Haiti&include_entities=1","count":2,"since_id":0,"since_id_str":"0"}}n";
JSONObject jsonObject = new JSONObject(str);
JSONArray jsonArray = jsonObject.getJSONArray("statuses");
System.out.println(jsonArray.getJSONObject(0).getString("created_at"));
System.out.println(jsonArray.getJSONObject(0).getString("text"));

相关内容

  • 没有找到相关文章

最新更新