Kav*_*veh 7 json ruby-on-rails
我从xml-rpc服务器得到了一个像这样的json文件:
{
"total_asr": -1,
"report": [
{
"logout_time_formatted": "2013-12-07 15:19",
"caller_id": "",
"user_id": x,
"bytes_in": "964826",
"login_time_formatted": "2013-12-07 13:46",
"details": [
[
"terminate_cause",
"Lost-Carrier"
],
[
"service",
"x"
],
[
"nas_port_type",
"Ethernet"
],
[
"mac",
"x:x:x:C6:63:DE"
],
[
"assigned_ip",
"x.x.65.139"
],
[
"port",
"x"
]
],
"ras_description": "x",
"service_type": "Internet",
"username": "x",
"successful": "t",
"bytes_out": "423928",
"before_credit": 10029.29841,
"connection_log_id": 49038,
"billed_duration": 5562,
"credit_used": 11.25756,
"retry_count": 1,
"parent_isp_credit_used": 0.0,
"duration_seconds": 5563.0,
"remote_ip": "5.200.65.139"
},
.
.
.
],
"total_rows": "85",
"total_voip_provider_credit_used": -1,
"total_credit": -1,
"total_duration": -1,
"total_out_bytes": -1.0,
"total_billed_duration": -1,
"total_in_bytes": -1.0,
"total_acd": -1
}
Run Code Online (Sandbox Code Playgroud)
我想得到一些结论:result = {}
rpc = IbsXmlRpc.new
res = rpc.getConnectionLogs("200")
result = res["report"].map{|key|
{
"duration" => (key["duration_seconds"].to_i/60).to_s,
"bytes_in" => (key["bytes_in"].to_i/1024).to_s,
"bytes_out" => (key["bytes_out"].to_i/1024).to_s,
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切正常,但是当我要获得json的最后一个字段时:
result["total_duration"] = res["total_duration"].to_s
result["total_out_bytes"] = res["total_out_bytes"].to_s
result["total_in_bytes"] = res["total_in_bytes"].to_s
Run Code Online (Sandbox Code Playgroud)
我得到这个错误:no implicit conversion of String into Integer
我真的不知道发生了什么,并测试了我所知道的一切.知道为什么会这样吗?
result不是哈希; 这是一个哈希数组.您无法通过String访问Array索引.如果要将总计存储在与"报告"相同的结构中,则需要将它们全部放在新的哈希中.
rpc = IbsXmlRpc.new
res = rpc.getConnectionLogs("200")
reports = res["report"].map{|key|
{
"duration" => (key["duration_seconds"].to_i/60).to_s,
"bytes_in" => (key["bytes_in"].to_i/1024).to_s,
"bytes_out" => (key["bytes_out"].to_i/1024).to_s,
}
}
result = {
:reports => reports,
:total_duration => res["total_duration"],
:total_out_bytes => res["total_out_bytes"],
:total_in_bytes => res["total_in_bytes"],
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9933 次 |
| 最近记录: |