首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拼音哈希parsed_response错误

拼音哈希parsed_response错误
EN

Stack Overflow用户
提问于 2013-02-11 08:31:04
回答 1查看 264关注 0票数 1

背景

我正在使用HTTParty来解析XML响应。不幸的是,当散列响应只有一个条目(?)时,产生的散列是不可索引的。我已经确认,对于单项和多项(?),生成的XML语法是相同的。我还确认了当总是有多个条目(?)时,我的代码可以工作。在散列中。

问题

我如何适应单个散列条目的情况和/或有没有更容易的方法来完成我想要做的事情?

代码

代码语言:javascript
复制
require 'httparty'

  class Rest
    include HTTParty
    format :xml
  end


  def test_redeye
    # rooms and devices
    roomID = Hash.new
    deviceID = Hash.new { |h,k| h[k] = Hash.new }
    rooms = Rest.get(@reIp["theater"] + "/redeye/rooms/").parsed_response["rooms"]
    puts "rooms #{rooms}"   
    rooms["room"].each do |room|
        puts "room #{room}"
        roomID[room["name"].downcase.strip] = "/redeye/rooms/" + room["roomId"] 
        puts "roomid #{roomID}" 
        devices = Rest.get(@reIp["theater"] + roomID[room["name"].downcase.strip] + "/devices/").parsed_response["devices"]
        puts "devices #{devices}"
        devices["device"].each do |device|
            puts "device #{device}"
            deviceID[room["name"].downcase.strip][device["displayName"].downcase.strip] = "/devices/" + device["deviceId"] 
            puts "deviceid #{deviceID}"
        end
    end 
    say "Done"
  end   

XML -单项

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<devices>
  <device manufacturerName="Philips" description="" portType="infrared" deviceType="0" modelName="" displayName="TV" deviceId="82" />
</devices>

XML -多个条目

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" ?>
<devices>
  <device manufacturerName="Denon" description="" portType="infrared" deviceType="6" modelName="Avr-3311ci" displayName="AVR" deviceId="77" />
  <device manufacturerName="Philips" description="" portType="infrared" deviceType="0" modelName="" displayName="TV" deviceId="82" />
</devices>

由此产生的错误

代码语言:javascript
复制
[Info - Plugin Manager] Matches, executing block
rooms {"room"=>[{"name"=>"Home Theater", "currentActivityId"=>"78", "roomId"=>"-1", "description"=>""}, {"name"=>"Living", "currentActivityId"=>"-1", "roomId"=>"81", "description"=>"2nd Floor"}, {"name"=>"Theater", "currentActivityId"=>"-1", "roomId"=>"80", "description"=>"1st Floor"}]}
room {"name"=>"Home Theater", "currentActivityId"=>"78", "roomId"=>"-1", "description"=>""}
roomid {"home theater"=>"/redeye/rooms/-1"}
devices {"device"=>[{"manufacturerName"=>"Denon", "description"=>"", "portType"=>"infrared", "deviceType"=>"6", "modelName"=>"Avr-3311ci", "displayName"=>"AVR", "deviceId"=>"77"}, {"manufacturerName"=>"Philips", "description"=>"", "portType"=>"infrared", "deviceType"=>"0", "modelName"=>"", "displayName"=>"TV", "deviceId"=>"82"}]}
device {"manufacturerName"=>"Denon", "description"=>"", "portType"=>"infrared", "deviceType"=>"6", "modelName"=>"Avr-3311ci", "displayName"=>"AVR", "deviceId"=>"77"}
deviceid {"home theater"=>{"avr"=>"/devices/77"}}
device {"manufacturerName"=>"Philips", "description"=>"", "portType"=>"infrared", "deviceType"=>"0", "modelName"=>"", "displayName"=>"TV", "deviceId"=>"82"}
deviceid {"home theater"=>{"avr"=>"/devices/77", "tv"=>"/devices/82"}}
room {"name"=>"Living", "currentActivityId"=>"-1", "roomId"=>"81", "description"=>"2nd Floor"}
roomid {"home theater"=>"/redeye/rooms/-1", "living"=>"/redeye/rooms/81"}
devices {"device"=>{"manufacturerName"=>"Philips", "description"=>"", "portType"=>"infrared", "deviceType"=>"0", "modelName"=>"", "displayName"=>"TV", "deviceId"=>"82"}}
device ["manufacturerName", "Philips"]
/usr/local/rvm/gems/ruby-1.9.3-p374@SiriProxy/gems/siriproxy-0.3.2/plugins/siriproxy-redeye/lib/siriproxy-redeye.rb:145:in `[]': can't convert String into Integer (TypeError)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-11 08:54:35

我看到了几种选择。如果您控制端点,则可以通过将type="array"属性放在devices XML元素上来修改发送到accomodate HTTParty's underlying XML parser, Crack的XML。

否则,您可以在索引到设备之前检查设备是什么类:

代码语言:javascript
复制
case devices["device"]
when Array
  # act on the collection
else
  # act on the single element
end

每当您必须在动态语言中进行类型检查时,它都不太理想,因此,如果您发现自己多次执行此操作,那么引入多态性或至少提取一个方法来执行此操作可能是值得的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14804307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档