gpt4 book ai didi

ruby-on-rails - Ruby on Rails : `no implicit conversion of String into Integer` for getting json field

转载 作者:行者123 更新时间:2023-12-04 06:27:59 26 4
gpt4 key购买 nike

我从 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
}

我想得到一些领域:
结果 = {}
    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,
}
}

到目前为止一切正常,但是当我要获取 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

我收到此错误: no implicit conversion of String into Integer我真的不知道发生了什么并测试了我所知道的一切。知道为什么会这样吗?

最佳答案

result不是哈希;它是一个哈希数组。您不能通过字符串访问数组索引。如果要将总计存储在与“报告”相同的结构中,则需要将它们全部放入新的哈希中。

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"],
}

关于ruby-on-rails - Ruby on Rails : `no implicit conversion of String into Integer` for getting json field,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450497/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com