2014-06-10 21 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']}"