我正在尝试通过RESTful API解析我从Twitter收到的JSON字符串作为响应。我想使用JSON gem,但我遇到了一个问题。根据文档,似乎JSON解析器返回Hash,这就是我想要的。但是,当我实际执行JSON.parse(twitter_result.body)时,它返回一个数组。如何让JSON解析器返回哈希?
这是我正在使用的解析器: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 :(得分:4)
JSON输出返回一个数组,其中包含一个您正在寻找的Hash元素。
试试这个:
puts JSON.parse(twitter_result.body)[0]