Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ruby中解析HttpParty get响应_Ruby_Json_Api_Parsing - Fatal编程技术网

在ruby中解析HttpParty get响应

在ruby中解析HttpParty get响应,ruby,json,api,parsing,Ruby,Json,Api,Parsing,我不知道如何解析这个响应。我只想从中提取一个计数 更新: 如何解析这个XML响应 这是我的API连接信息 我正在使用Ruby irb和HttpParty require 'httparty' api_key = "6e569b3d-3457-9485-edb2-eccd27272dbf" secret_key = "MDVkYTQzMDgtOWRiOC04NDULTRkYWUtODQwOTc5ZDFkZTQ3NTZhOG1NWQtNmMzMi0xYjE0LWYMzgtNDc0YzU0MWYxY

我不知道如何解析这个响应。我只想从中提取一个计数

更新: 如何解析这个XML响应

这是我的API连接信息

我正在使用Ruby irb和HttpParty

require 'httparty'

api_key = "6e569b3d-3457-9485-edb2-eccd27272dbf"
secret_key = "MDVkYTQzMDgtOWRiOC04NDULTRkYWUtODQwOTc5ZDFkZTQ3NTZhOG1NWQtNmMzMi0xYjE0LWYMzgtNDc0YzU0MWYxYmUx"

def urlncode(string)
  URI.escape(string, Regexp.new("[Generating an API Signature^#{URI::PATTERN::UNRESERVED}]"))
end

salt = rand(10000000000).to_s

hash = OpenSSL::HMAC.digest('sha256', secret_key, salt)

signature = urlncode(Base64.encode64(hash))

# controller / action
api_path = "/Tickets/TicketCount"

url = "http://support.myorganization.org/api/index.php?e=#{api_path}"

response = HTTParty.get("#{url}&apikey=#{api_key}&salt=#{salt}&signature=#{signature}").parsed_response
我的回答是:被截断了

response = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<ticketcount>\n<departments>\n<department id=\"0\">\n<totalitems><![CDATA[11]]></totalitems>\n<lastactivity><![CDATA[1423657406]]></lastactivity>\n<totalunresolveditems><![CDATA[11]]></totalunresolveditems>\n<ticketstatus id=\"1\" lastactivity=\"1423657406\" totalitems=\"6\" />\n<ticketstatus id=\"2\" lastactivity=\"1422566730\" totalitems=\"5\" />\n<tickettype id=\"1\" lastactivity=\"1423657406\" totalitems=\"11\" totalunresolveditems=\"11\" />\n<ownerstaff id=\"0\" lastactivity=\"1423657406\" totalitems=\"7\" totalunresolveditems=\"7\" />\n<ownerstaff id=\"4\" lastactivity=\"1420825202\" totalitems=\"2\" totalunresolveditems=\"2\" />\n<ownerstaff id=\"17\" lastactivity=\"1422452400\" totalitems=\"1\" totalunresolveditems=\"1\" />\n<ownerstaff id=\"26\" lastactivity=\"1422566730\" totalitems=\"1\"
我明白了:这显然不是所有的数据

irb(main):029:0> data['ticketcount']
=> "ticketcount"

您没有调用正确的端点,您的响应是XML而不是json。检查你要点击的端点,你能再详细一点吗;不知道你说的是什么意思你想去http://support.myorganization.org/api/index.php?e=/Tickets/TicketCount 对吗?正确,URL参数中包含api信息。如果XML不应该是data=response.parsed_response,然后是data['ticketcount']?它是XML而不是json,那么您不能将XML解析为json
irb(main):029:0> data['ticketcount']
=> "ticketcount"