如何仅打印json中的文本。我正在使用rails app上的ruby。
tweets = JSON.parse(response.body)
tweets.each do |tweet|
"#{tweet["text"]}"
end
我尝试了上面的代码,它显示了整个json。
Response.body:
[{" created_at" =>" Mon Jun 09 02:49:17 +0000 2014", " id" => 475831972812423168," id_str" =>" 475831972812423168", " text" =>" @debalec#DIST1"," source" =>" https://dev.twitter.com/docs/ TFW \" rel = \" nofollow \"> Twitter for 网站","截断" => false," in_reply_to_status_id" => nil, " in_reply_to_status_id_str" => nil," in_reply_to_user_id" => 2551123651, " in_reply_to_user_id_str" = GT;" 2551123651&#34 ;, " in_reply_to_screen_name" =>" debalec"," user" => {" id" => 1551421092, " id_str" =>" 1551421092"," name" =>" The Third", " screen_name" =>"第三个","位置" =>"","说明" => ;"&#34 ;, " url" => nil," entity" => {" description" => {" urls" => [] }}, " protected" => false," followers_count" => 12," friends_count" => 199, " listed_count" => 0," created_at" =>" Thu Jun 27 20:24:56 +0000 2013", " favourites_count" => 54," utc_offset" => nil," time_zone" => nil, " geo_enabled" => false,"已验证" => false," statuses_count" => 82, " lang" =>" zh"," contributors_enabled" => false," is_translator" => false, " is_translation_enabled" => false," profile_background_color" =>" 59BEE4", " profile_background_image_url" = GT;" http://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg&#34 ;, " profile_background_image_url_https" = GT;" https://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg&#34 ;, " profile_background_tile" = GT;假, " profile_image_url" = GT;" http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png&#34 ;, " profile_image_url_https" = GT;" https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png&#34 ;, " profile_link_color" = GT;" 8FCAE0&#34 ;, " profile_sidebar_border_color" = GT;" 4BB7DF&#34 ;, " profile_sidebar_fill_color" = GT;" 191F22&#34 ;, " profile_text_color" =>" 4BB7DF"," profile_use_background_image" => true, " default_profile" => false," default_profile_image" => true, "关注" => false," follow_request_sent" => false, "通知" => false}," geo" => nil,"坐标" => nil,"地点" => ;零, "贡献者" => nil," retweet_count" => 0," favorite_count" => 0, "实体" => {" hashtags" => [{" text" =>" DIST1"," indices& #34; => [10,16]}], "符号" => []," urls" => [], " user_mentions" => [{" screen_name" =>" debalec"," name" =>" DebaElec&# 34 ;, " id" => 2551123651," id_str" =>" 2551123651"," indices" => [0,8]} ]}, " favited" => false,"转发" => false," lang" =>" und"}, {" created_at" =>" Fri Jun 06 22:41:39 +0000 2014", " id" => 475044876841938944," id_str" =>" 475044876841938944"," text" =>" hi @ debalec"," source" =>" http://twitter.com/download/android \" rel = \" nofollow \"> Twitter for Android","截断" => false, " in_reply_to_status_id" => nil," in_reply_to_status_id_str" => nil, " in_reply_to_user_id" => nil," in_reply_to_user_id_str" => nil, " in_reply_to_screen_name" => nil," user" => {" id" => 1551421092, " id_str" =>" 1551421092"," name" =>"第三个", " screen_name" =>"第三个","位置" =>"","说明" = >"&#34 ;, " url" => nil," entity" => {" description" => {" urls" => [] }}, " protected" => false," followers_count" => 12," friends_count" => 199, " listed_count" => 0," created_at" =>" Thu Jun 27 20:24:56 +0000 2013", " favourites_count" => 54," utc_offset" => nil," time_zone" => nil, " geo_enabled" => false,"已验证" => false," statuses_count" => 82, " lang" =>" zh"," contributors_enabled" => false," is_translator" => false, " is_translation_enabled" => false," profile_background_color" =>" 59BEE4", " profile_background_image_url" =>" http://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg"" profile_background_image_url_https" =>" https://pbs.twimg.com/profile_background_images/378800000083038715/7b1cad0896d22d75b85f5f86fc69b59f.jpeg&#34 ;, " profile_background_tile" =>假" profile_image_url" =>" http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png"" profile_image_url_https" =>& #34; https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png&#34 ;, " profile_link_color" = GT;" 8FCAE0&#34 ;, " profile_sidebar_border_color" = GT;" 4BB7DF&#34 ;, " profile_sidebar_fill_color" = GT;" 191F22&#34 ;, " profile_text_color" =>" 4BB7DF"," profile_use_background_image" => true, " default_profile" => false," default_profile_image" => true, "关注" => false," follow_request_sent" => false, "通知" => false}," geo" => nil,"坐标" => nil,"地点" => ;零, "贡献者" => nil," retweet_count" => 0," favorite_count" => 0, "实体" => {" hashtags" => [],"符号" => []," urls" => ; [], " user_mentions" => [{" screen_name" =>" debalec"," name" =>" DebaElec&# 34 ;, " id" => 2551123651," id_str" =>" 2551123651"," indices" => [3,11]} ]}, " favited" => false,"转发" => false," lang" =>" und"}]
答案 0 :(得分:0)
这里的一个问题是您在引号内使用引号。忘了逃跑?
另一个问题是你没有打印任何东西。仅仅在循环中返回文本什么都不做,并且在循环完成时总是会返回tweets
对象。
所以,你走了:
tweets = JSON.parse(response.body)
tweets.each do |tweet|
puts tweet["text"]
end
<强>更新强>
好吧,我猜这里引号内的引号只要你使用字符串插值就不会有问题。但它仍然不是首选方式。我宁愿选择"#{tweet['text']}"
。
答案 1 :(得分:0)
使用map
代替each
:
tweets = JSON.parse(response.body)
tweets.map do |tweet|
tweet["text"]
end
并且无需插入tweet["text"]
。