我从 Pivotal Tracker API 获取 JSON 信息,我需要获取某些信息,而不是所有原始数据。为此,我曾经JSON.parse()
将 JSON 转换为 Ruby 哈希数组。
现在,我需要遍历这个数组并且只返回相关的哈希值。
例如,我只想返回primary_resources
具有嵌套哈希的哈希:
"story_type" => "feature"
为此,我编写了以下代码(response.body
是从 GET 请求返回的数据):
@data = response.body
parsed = JSON.parse(@data)
puts parsed['primary_resources']['story_type']['feature']
当我运行脚本时,我收到此错误:
no implicit conversion of String into Integer (TypeError)
似乎它正在遍历哈希数组并寻找数组的整数(如array[3]
or array[0]
),但这对我没有帮助。我需要返回:kind => story
在主资源散列中具有嵌套散列的所有散列。
我也尝试过这样做:
parsed.each do |entry|
puts entry['primary_resources']['story_Type']['feature']
end
我得到了同样的错误。
这是我的完整代码:
require 'json'
require 'net/http'
require 'open-uri'
require 'openssl'
require 'active_support'
prompt = '> '
puts "What is the id of the Project you want to get data from?"
print prompt
project_id = STDIN.gets.chomp()
puts "What is the beginning date you want to return from? ex: 2014-07-02"
print prompt
date1 = STDIN.gets.chomp()
puts "What is the end date you want to return from? ex: 2014-07-16"
print prompt
date2 = STDIN.gets.chomp()
def scope(project_id, date1, date2)
uri = URI.parse("https://www.pivotaltracker.com/services/v5/projects/#{project_id}/activity?occurred_after=#{date1}T01:00:15Z&occurred_before=#{date2}T01:00:15Z&fields=primary_resources")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri.request_uri)
request.add_field("X-TrackerToken", "*****************")
response = http.request(request)
@data = response.body
parsed = JSON.parse(@data)
puts parsed['primary_resources']['story_type']['feature']
# parsed.each do |entry|
# puts entry['primary_resources']['kind']['story']
# end
# puts parsed['primary_resources'].select { |r| r['story_type'].eql? 'feature' }
end
scope(project_id, date1, date2)
这是一些没有解析的 JSON 响应(完整的响应太长了,对于其他 15 个用户故事来说实际上只是相同的响应):
What is the id of the Project you want to get data from?
> 961142
What is the beginning date you want to return from? ex: 2014-07-02
> 2014-07-02
What is the end date you want to return from? ex: 2014-07-16
> 2014-07-03
[
{
"primary_resources": [
{
"kind": "story",
"id": 74313670,
"name": "User can make an image fit inside the grid when viewing image detail and save it to case template. BUILD 146",
"story_type": "bug",
"url": "https://www.pivotaltracker.com/story/show/74313670"
}
],
"guid": "961142_3419",
"project_version": 3419
},
这是解析后的一些 JSON 响应(出于与上述相同的原因,仅显示第一个故事):
What is the id of the Project you want to get data from?
> 961142
What is the beginning date you want to return from? ex: 2014-07-02
> 2014-07-02
What is the end date you want to return from? ex: 2014-07-16
> 2014-07-03
{"primary_resources"=>[{"kind"=>"story", "id"=>74313670, "name"=>"User can make an image fit inside the grid when viewing image detail and save it to case template. BUILD 146", "story_type"=>"bug", "url"=>"https://www.pivotaltracker.com/story/show/74313670"}], "guid"=>"961142_3419", "project_version"=>3419}
如何遍历这个哈希数组并只返回 "story_type"=>"feature" 的那些?