2013-12-08 13 views
6

Tôi đang cố gắng phân tích chuỗi JSON mà tôi nhận được từ Twitter dưới dạng phản hồi qua API RESTful. Tôi muốn sử dụng đá quý JSON, nhưng tôi đã gặp phải vấn đề. Theo tài liệu, có vẻ như trình phân tích cú pháp JSON trả về một Hash, đó là những gì tôi muốn. Tuy nhiên, khi tôi thực sự thực hiện JSON.parse (twitter_result.body), nó trả về một mảng. Làm thế nào tôi có thể nhận được trình phân tích cú pháp JSON để trả về một Hash?Ruby JSON.parse trả về một Array

Đây là phân tích cú pháp Tôi đang sử dụng: http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parse

"Pretty" JSON đầu ra ví dụ:

[ 
    { 
    "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

bài viết 'twitter_result.body' và sản lượng phân tích cú pháp. – rohit89

+0

Đầu ra Parser chỉ là một đối tượng. Làm thế nào tôi nên thể hiện điều đó? – csnate

+2

Tất nhiên nó trả về một mảng, JSON của bạn bắt đầu bằng '[' – Stefan

Trả lời

3

JSON đầu ra trả về một mảng với một yếu tố đó là Hash bạn đang tìm kiếm.

Hãy thử điều này:

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

Các vấn đề liên quan