2013-12-08 57 views
6

我試圖解析一個JSON字符串,我通過RESTful API從Twitter收到的響應作爲響應。我想使用JSON寶石,但我遇到了一個問題。根據文檔,看起來JSON解析器返回一個哈希,這正是我想要的。但是,當我實際執行JSON.parse(twitter_result.body)時,它會返回一個Array。我怎樣才能讓JSON解析器返回一個哈希?Ruby JSON.parse返回一個Array

這是我使用的解析器:http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parse

「漂亮」 JSON輸出示例:

[ 
    { 
    "created_at": "Sun Dec 08 13:00:52 +0000 2013", 
    "id": 409668902189137920, 
    "id_str": "409668902189137920", 
    "text": "\"All the people departed, and David went home to bless his household.\" 1 Chron. 16:43. Big meeting. Blessed home. Both.", 
    "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</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": 27500565, 
     "id_str": "27500565", 
     "name": "John Piper", 
     "screen_name": "JohnPiper", 
     "location": "Minneapolis, MN", 
     "description": "Founder-Teacher, Desiring God, Chancellor, BCS. Spreading a passion for the supremacy of God in all things for the joy of all peoples through Jesus Christ.", 
     "url": "http://t.co/qt1rUlRfdl", 
     "entities": { 
     "url": { 
      "urls": [ 
      { 
       "url": "http://t.co/qt1rUlRfdl", 
       "expanded_url": "http://www.desiringgod.org", 
       "display_url": "desiringgod.org", 
       "indices": [ 
       0, 
       22 
       ] 
      } 
      ] 
     }, 
     "description": { 
      "urls": [ 

      ] 
     } 
     }, 
     "protected": false, 
     "followers_count": 564749, 
     "friends_count": 107, 
    [ 
    { 
    "created_at": "Sun Dec 08 13:00:52 +0000 2013", 
    "id": 409668902189137920, 
    "id_str": "409668902189137920", 
    "text": "\"All the people departed, and David went home to bless his household.\" 1 Chron. 16:43. Big meeting. Blessed home. Both.", 
    "source": "<a href=\"http://www.hootsuite.com\" rel=\"nofollow\">HootSuite</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": 27500565, 
     "id_str": "27500565", 
     "name": "John Piper", 
     "screen_name": "JohnPiper", 
     "location": "Minneapolis, MN", 
     "description": "Founder-Teacher, Desiring God, Chancellor, BCS. Spreading a passion for the supremacy of God in all things for the joy of all peoples through Jesus Christ.", 
     "url": "http://t.co/qt1rUlRfdl", 
     "entities": { 
     "url": { 
      "urls": [ 
      { 
       "url": "http://t.co/qt1rUlRfdl", 
       "expanded_url": "http://www.desiringgod.org", 
       "display_url": "desiringgod.org", 
       "indices": [ 
       0, 
       22 
       ] 
      } 
      ] 
     }, 
     "description": { 
      "urls": [ 

      ] 
     } 
     }, 
     "protected": false, 
     "followers_count": 564749, 
     "friends_count": 107, 
     "listed_count": 9225, 
     "created_at": "Sun Mar 29 20:48:44 +0000 2009", 
     "favourites_count": 20, 
     "utc_offset": -21600, 
     "time_zone": "Central Time (US & Canada)", 
     "geo_enabled": true, 
     "verified": true, 
     "statuses_count": 7906, 
     "lang": "en", 
     "contributors_enabled": false, 
     "is_translator": false, 
     "profile_background_color": "570000", 
     "profile_background_image_url": "http://a0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg", 
     "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg", 
     "profile_background_tile": false, 
     "profile_image_url": "http://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg", 
     "profile_image_url_https": "https://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg", 
     "profile_link_color": "5C0606", 
     "profile_sidebar_border_color": "000000", 
     "profile_sidebar_fill_color": "CCC2A3", 
     "profile_text_color": "000000", 
     "profile_use_background_image": true, 
     "default_profile": false, 
     "default_profile_image": false, 
     "following": true, 
     "follow_request_sent": false, 
     "notifications": false 
    }, 
    "geo": null, 
    "coordinates": null, 
    "place": null, 
    "contributors": null, 
    "retweet_count": 20, 
    "favorite_count": 31, 
    "entities": { 
     "hashtags": [ 

     ], 
     "symbols": [ 

     ], 
     "urls": [ 

     ], 
     "user_mentions": [ 

     ] 
    }, 
    "favorited": false, 
    "retweeted": false, 
    "lang": "en" 
    } 
] "listed_count": 9225, 
     "created_at": "Sun Mar 29 20:48:44 +0000 2009", 
     "favourites_count": 20, 
     "utc_offset": -21600, 
     "time_zone": "Central Time (US & Canada)", 
     "geo_enabled": true, 
     "verified": true, 
     "statuses_count": 7906, 
     "lang": "en", 
     "contributors_enabled": false, 
     "is_translator": false, 
     "profile_background_color": "570000", 
     "profile_background_image_url": "http://a0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg", 
     "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/38963234/jp_twitter_1920x1200_b_2.jpg", 
     "profile_background_tile": false, 
     "profile_image_url": "http://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg", 
     "profile_image_url_https": "https://pbs.twimg.com/profile_images/1563845055/2010fallJPmug_normal.jpeg", 
     "profile_link_color": "5C0606", 
     "profile_sidebar_border_color": "000000", 
     "profile_sidebar_fill_color": "CCC2A3", 
     "profile_text_color": "000000", 
     "profile_use_background_image": true, 
     "default_profile": false, 
     "default_profile_image": false, 
     "following": true, 
     "follow_request_sent": false, 
     "notifications": false 
    }, 
    "geo": null, 
    "coordinates": null, 
    "place": null, 
    "contributors": null, 
    "retweet_count": 20, 
    "favorite_count": 31, 
    "entities": { 
     "hashtags": [ 

     ], 
     "symbols": [ 

     ], 
     "urls": [ 

     ], 
     "user_mentions": [ 

     ] 
    }, 
    "favorited": false, 
    "retweeted": false, 
    "lang": "en" 
    } 
] 
+0

後'twitter_result.body'和分析器輸出。 – rohit89

+0

解析器輸出只是一個對象。我應該如何顯示? – csnate

+2

當然它返回一個數組,你的JSON以'['' – Stefan

回答

3

JSON輸出將返回這是你要找的哈希一個元素的數組。

試試這個:

puts JSON.parse(twitter_result.body)[0]