Ruby打印JSON值-没有将String隐式转换为Integer



我试图只打印JSON的值,结果得到错误no implicit conversion of String into Integer

知道我该怎么解决这个问题吗?

puts test.class

Azure::Armrest::ArmrestCollection

这是可变值的输出,我只想打印quantity值。

puts test

[----] I, [2022-08-23T08:59:20.279357 #550:bc494]  INFO -- automation: Method STDOUT: {"id":"/subscriptions/123456/providers/Microsoft.Commerce/UsageAggregates/Daily_BRSDT_20220801_0000","name":"Daily_BRSDT_20220801_0000","type":"Microsoft.Commerce/UsageAggregate","properties":{"subscriptionId":"123456","usageStartTime":"2022-07-31T00:00:00+00:00","usageEndTime":"2022-08-01T00:00:00+00:00","meterName":"Data Transfer Out","meterRegion":"North America","meterCategory":"Bandwidth","meterSubCategory":"Inter-Region","unit":"1 GB","instanceData":"{"Microsoft.Resources":{"resourceUri":"/subscriptions/123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Compute/virtualMachines/ubuntu975","location":"eastus","additionalInfo":{"PipelineType":"v2","DataTransferDirection":"DataTrOut"}}}","meterId":"dbefcfc1-e3f6-409b-be6d-9cd7b00724a5","infoFields":{},"quantity":9.052455425262451e-07}}
[----] I, [2022-08-23T08:59:20.280435 #550:bc494]  INFO -- automation: Method STDOUT: {"id":"/subscriptions/123456/providers/Microsoft.Commerce/UsageAggregates/Daily_BRSDT_20220801_0000","name":"Daily_BRSDT_20220801_0000","type":"Microsoft.Commerce/UsageAggregate","properties":{"subscriptionId":"123456","usageStartTime":"2022-07-31T00:00:00+00:00","usageEndTime":"2022-08-01T00:00:00+00:00","meterName":"Dynamic Public IP","meterCategory":"Virtual Network","meterSubCategory":"IP Addresses","unit":"1 Hour","instanceData":"{"Microsoft.Resources":{"resourceUri":"/subscriptions/123456/resourceGroups/cloud-shell-storage-centralindia/providers/Microsoft.Network/publicIPAddresses/Ubuntu975-publicIp","location":"eastus","additionalInfo":{"IpAddress":"20.25.57.9","PlatformType":"V2-Agg"}}}","meterId":"f114cb19-ea64-40b5-bcd7-aee474b62853","infoFields":{},"quantity":1.0}}

puts test["properties"]["quantity"]

[----] E, [2022-08-23T08:59:28.949873 #544:b6620] ERROR -- automation: <AEMethod getcostusage> [no implicit conversion of String into Integer]
/CloudForms_Essentials/Integration/Azure/StateMachines/getCostUsage:79:in `[]'
/CloudForms_Essentials/Integration/Azure/StateMachines/getCostUsage:79:in `<main>'
[----] I, [2022-08-23T08:59:28.974301 #544:ac260]  INFO -- automation: <AEMethod [/CloudForms_Essentials/Integration/Azure/StateMachines/getCostUsage]> Ending

如果我尝试JSON.parse(测试(,那么我会得到这个错误。

[----] E, [2022-08-23T09:18:39.022825 #553:bcaac] ERROR -- automation: <AEMethod getcostusage> [no implicit conversion of Azure::Armrest::ArmrestCollection into String]

testArmrestCollection的一个实例,它继承自Array类(可以在此处查看(。

要打印所有元素的数量,可以对其进行迭代并分别打印:

test.each do |element|
puts element["properties"]["quantity"]
end

最新更新