0

尝试格式化从 meetup.com API 收到的一些字段。我可以获得信息,但我无法将其格式化为表格或其他易于查看的格式。

get '/ruby' do
  api_result = RestClient.get (Addressable::URI.parse('http://api.meetup.com/groups.json/?zip=14226&topic=ruby&order=members&key=XXXXXX').normalize.to_str) 
  puts api_result["name"]["city"]
end

我得到的唯一输出是空白。如果我删除 puts 行并使用 erb :meetup_table, :locals => {result: api_result}

我明白了:

{
   "results":[
      {
         "visibility":"public",
         "link":"http:\/\/www.meetup.com\/Western-New-York-Ruby\/",
         "state":"NY",
         "organizer_id":"8354132",
         "join_mode":"0",
         "city":"Buffalo",
         "who":"Rubyists",
         "id":"1320047",
         "created":"Wed Nov 12 09:34:40 EST 2008",
         "updated":"Thu Aug 22 10:46:04 EDT 2013",
         "description":"WNY Ruby User Group\nRuby programming meetup in Buffalo, NY. We just started on meetup.com, but have been meeting for a year. Come join us, or sign up to RSVP if you're a regular member!\n#NYRUBY irc\nJoin us on Freenode irc server in channel #nyruby",
         "name":"WNY \/ Buffalo Ruby",
         "members":"207",
         "lat":"42.91999816894531",
         "group_photo_count":"0",
         "zip":"14222",
         "lon":"-78.87999725341797",
         "join_info":"None",
         "photo_url":"http:\/\/photos1.meetupstatic.com\/photos\/event\/5\/5\/5\/global_6289365.jpeg",
         "group_urlname":"Western-New-York-Ruby",
         "organizer_name":"Wayne E. Seguin",
         "short_link":"",
         "country":"us",
         "topics":[
            {
               "id":"188",
               "urlkey":"linux",
               "name":"Linux"
            },
            {
               "id":"563",
               "urlkey":"opensource",
               "name":"Open Source"
            },
            {
               "id":"659",
               "urlkey":"webdesign",
               "name":"Web Design"
            },
            {
               "id":"1040",
               "urlkey":"ruby",
               "name":"Ruby"
            },
            {
               "id":"10209",
               "urlkey":"web",
               "name":"Web Technology"
            },
            {
               "id":"10579",
               "urlkey":"technology",
               "name":"Internet & Technology"
            },
            {
               "id":"15582",
               "urlkey":"web-development",
               "name":"Web Development"
            },
            {
               "id":"17627",
               "urlkey":"programming",
               "name":"Programming"
            },
            {
               "id":"20837",
               "urlkey":"ruby-on-rails",
               "name":"Ruby On Rails"
            }
         ],
         "organizerProfileURL":"http:\/\/www.meetup.com\/members\/8354132\/",
         "rating":"4.60",
         "daysleft":""
      }
   ],
   "meta":{
      "lon":-78.80000305175781,
      "count":1,
      "link":"http:\/\/api.meetup.com\/groups.json\/",
      "next":"",
      "total_count":1,
      "url":"http:\/\/api.meetup.com\/groups.json\/?key=682d733452163d471f4656620674a53&radius=25.0&topic=ruby&order=members&offset=0&format=json&page=200&lat=42.9700012207&zip=14226&fields=&lon=-78.8000030518",
      "id":"",
      "title":"Meetup Groups",
      "updated":"Thu Aug 22 10:46:04 EDT 2013",
      "description":"API method for accessing meetup groups",
      "method":"Groups",
      "lat":42.970001220703125
   }
}
4

1 回答 1

0

JSON 数据只是一个字符串,所以你不能像散列一样使用它,除非你用它做一个散列:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/json/rdoc/JSON.html

json = RestClient.get ...
jhash = JSON.parse(json)
puts jhash['results'][0]['city'] # now we're a hash
于 2013-08-22T15:36:13.753 回答