我将需要访问数组以检索信息并将其插入数据库(代码,客户,电话1,电话2)。有人可以帮助我吗?
{ :recordset => { :row => [ [0] { :property => [ [0] { :name => "Code", :value => "C0001" }, [1] { :name => "Customer", :value => "ROSSI MARIO" }, [2] { :name => "Phone1", :value => "1234567890" } ] }, [1] { :property => [ [0] { :name => "Code", :value => "C0002" }, [1] { :name => "Customer", :value => "VERDE VINCENT" }, [2] { :name => "Phone1", :value => "9876543210" }, [3] { :name => "Phone2", :value => "2468101214" } ] } ] }, :@xmlns => "http://localhost/test" }
p.s。 SOAP调用期间的Phone2值仅在存档中存在时才显示
谢谢
答案 0 :(得分:0)
您的数据看起来不像是有效的红宝石结构。一旦能够将其转换为更像红宝石的颜色,就可以使用each_with_object
从数据中生成格式良好的属性集及其值:
data
=> {:recordset=>{
:row=>[{
:property=>[
{:name=>"Code", :value=>"C0001"},
{:name=>"Customer", :value=>"ROSSI MARIO"},
{:name=>"Phone1", :value=>"1234567890"}
]
}, {
:property=>[
{:name=>"Code", :value=>"C0002"},
{:name=>"Customer", :value=>"VERDE VINCENT"},
{:name=>"Phone1", :value=>"9876543210"},
{:name=>"Phone2", :value=>"2468101214"}
]
}]
},
:@xmlns=>"http://localhost/test"}
data.keys
=> [:recordset, :@xmlns]
data[:recordset][:row].count
=> 2 # There are 2 set of attribute-value pairs
result = data[:recordset][:row].each_with_object([]) do |hash, out|
out << hash[:property].each_with_object({}) do |h, o|
o[h[:name]] = h[:value]
end
end
=> [{"Code"=>"C0001", "Customer"=>"ROSSI MARIO", "Phone1"=>"1234567890"},
{"Code"=>"C0002", "Customer"=>"VERDE VINCENT", "Phone1"=>"9876543210", "Phone2"=>"2468101214"}]
现在,您可以遍历result
并在数据库中创建相应的记录。