Ruby JSON.parse 返回一个数组

标签 ruby json parsing twitter

我正在尝试通过 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"
  }
]

最佳答案

JSON 输出返回一个数组,其中一个元素就是您要查找的哈希。

试试这个:

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

关于Ruby JSON.parse 返回一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20454417/

相关文章:

ruby - 在 Ruby 中拆分一串具有不同数字大小的数字

ruby-on-rails - 在 rails 中查看演示者

ruby-on-rails - 在 Rails 中创建分类页面

java - 计算 JSONArray 中的字符串和 JSONObject 的数量

arrays - 使用 D3js v4 的带有 d3.hierarchy 的旭日图数据

java - 从嵌套的 JSON 对象中检索值

javascript - 如何从rails中的javascript调用 Controller 方法

java - Json 中的控制字符导致 JsonParsingException

xml - 验证 Flex/actionscript 的 XML 解析器?

java - 如何将包含 double 值的 char 数组转换为 double 变量,而不创建 String 对象的开销?