2013-12-08 9 views
6

私はRESTful API経由でTwitterから受け取ったJSON文字列を応答として解析しようとしています。私はJSONの宝石を使いたいと思っていましたが、私は問題にぶつかりました。ドキュメンテーションごとに、JSONパーサがハッシュを返しているように見えますが、これは私が望むものです。しかし、実際にJSON.parse(twitter_result.body)を実行すると、配列が返されます。 JSONパーサにハッシュを返すにはどうすればよいですか? http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html#method-i-parseRuby JSON.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

Parser出力は単なるオブジェクトです。私はそれをどのように示すべきですか? – csnate

+2

もちろん、配列を返します。あなたのJSONは '[' – Stefan

答えて

3

JSON出力は、あなたが探しているハッシュある一つの要素を持つ配列を返します。

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

関連する問題