2014-06-10 20 views
-1

如何仅打印json中的文本。我正在开发Ruby应用程序。从ruby中打印json中的特定对象

tweets = JSON.parse(response.body) 
    tweets.each do |tweet| 
      "#{tweet["text"]}" 
    end 

我试过上面的代码bt它显示了整个json。

Response.body:

[{ “created_at”=> “星期一6月09 2点49分17秒0000 2014”, “ID”=> 475831972812423168 “ID_STR”=>“475831972812423168 “, ”text“=>”@ debalec#DIST1“,”source“=>”https://dev.twitter.com/docs/tfw \“rel = \”nofollow \“> Twitter为 网站” “截短的”=>假 “in_reply_to_status_id”=>零, “in_reply_to_status_id_str”=>零, “in_reply_to_user_id”=> 2551123651, “in_reply_to_user_id_str”=> “2551123651”, “in_reply_to_screen_name”=> “debalec”,“ user“=> {”id“=> 1551421092, ”id_str“=>”15 51421092“,”name“=>”The Third“, ”screen_name“=>”thethird“,”location“=>”“,”description“=>”“,”url“=> => {“description”=> {“urls”=> []}}, “protected”=> false,“followers_count”=> 12,“friends_count”=> 199, “listed_count”=> created_at“=>”Thu Jun 27 20:24:56 +0000 2013“, ”favourites_count“=> 54,”utc_offset“=> nil,”time_zone“=>无, ”geo_enabled“=> false,”verified “=> false,”statuses_count“=> 82, ”lang“=>”en“,”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 “ ”profile_background_tile“=>假, ”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”=> “8FCAE0”, “profile_sidebar_border_color”=> “4BB7DF”, “profile_sidebar_fill_color”=> “191F22”, “profile_text_color”=> “4BB7DF”, “profile_use_background_image”=>真, “DEFAULT_PROFILE”=>假 “default_profile_image”=>真, “下面的”=>假, “follow_request_sent”=> false, “notifications”=> false},“geo”=> nil,“coordinates”=> nil,“place”=> nil, “contributors”=> nil,“retweet_count”=> 0,“favorite_count”=> 0, “entities”=> {“hashtags”=> [{“text”=>“DIST1”,“indi “=”[]“], ”symbols“=> [],”urls“=> [], ”user_mentions“=> [{”screen_name“=>”debalec“,”name“ >“DebaElec”, “id”=> 2551123651,“id_str”=>“2551123651”,“indices”=> [0,8]}]}, “favorited”=> false,“retweeted”=> 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”,“truncated”=> false, “in_reply_to_status_id”=> nil,“in_reply_to_status_id_str”=>无, “in_reply_to_user_id”=>无,“in_reply_to_user_id_str”=>无, “in_reply_to_screen_name”=>无,“user”=> {“id”=> 1551421092, “id_str”=>“1551421092”,“name”=>“第三个”, “screen_name”=>“第三个”,“location”=>“”,“description”=>“”, “url”=> nil,“entities”=> {“description”=> {“url” > []}}, “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“=>无, ”geo_enabled“=> false,”veri fied“=> false,”statuses_count“=> 82, ”lang“=>”en“,”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”, “profile_background_tile”=>假 “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”=> “8FCAE0”, “profile_sidebar_border_color”=> “4BB7DF”, “profile_sidebar_fill_color”=> “191F22”, “profile_text_color”=> “4BB7DF”, “profile_use_background_image”=>真, “DEFAULT_PROFILE” => false,“default_profile_image”=> true, “following”=> false,“follow_request_sent”=> false, “notifications”=> false},“geo”=> nil,“coordinates”=> nil,“place”=> nil, “contributors”= > nil,“retweet_count”=> 0,“favorite_count”=> 0, “entities”=> {“hashtags”=> [],“symbols”=> [],“urls”=> [] “ user_mentions“=> [{”screen_name“=>”debalec“,”name“=>”DebaElec“, ”id“=> 2551123651,”id_str“=>”2551123651“,”indices“=> [3,11 ]}]}, “收藏”=>假, “转推”=>假, “郎”=> “UND”}]这里

+0

请粘贴response.body –

+0

您正在试图打印结果的位置?意味着在erb文件或其他地方? –

+0

@SagarBommidi是的,我试图在erb上打印。 –

回答

0

使用map代替each

tweets = JSON.parse(response.body) 
tweets.map do |tweet| 
    tweet["text"] 
end 

而且也没有必要进行插值tweet["text"]

0

的一个问题是,你正在使用引号内的引号。忘了逃脱?

另一个问题是你不打印任何东西。仅仅在循环中返回文本什么也不做,并且在循环完成时总是返回tweets对象。

所以,在这里你去:

tweets = JSON.parse(response.body) 

tweets.each do |tweet| 
    puts tweet["text"] 
end 

更新:

嗯,我想引号内的行情不会在这里是一个问题,只要你使用字符串插值。但它仍然不是首选的方式。我宁愿去与"#{tweet['text']}"