这个问题与this question类似,并且得到了this question的帮助,但我认为我申请的json解析应该有效:
println(json.value[i].properties.instanceData)
结果
{
"Microsoft.Resources" : {
"location" : "Asiseast",
"tags" : {
"costCentre" : "2222",
"Department" : "DEPT",
"Project" : "IaaS"
},
"resourceUri" : "/subscriptions/xxx-xxx-xxx-xxx/resourceGroups/RGNAME/providers/Microsoft.Compute/virtualMachines/VMNAME",
"additionalInfo" : {
"ServiceType" : "",
"ImageType" : "",
"VMProperties" : "",
"UsageType" : "DataTrOut",
"VMName" : ""
}
}
}
我尝试使用以下查询来提取“Microsoft-Resources”的值 -
println(json.value[i].properties.instanceData["Microsoft.Resources"])
但我收到以下回复:
Caught: groovy.lang.MissingPropertyException: No such property: Microsoft.Resources for class: java.lang.String
尝试 -
println(json.value[i].properties.instanceData."Microsoft.Resources")
结果:
Caught: groovy.lang.MissingPropertyException: No such property: Microsoft.Resources for class: java.lang.String
供参考,输出到
println(json.value[i].properties)
是
[subscriptionId:xxx-xxx-xxx-xxx-xxx,
usageStartTime:2018-01-01T00:00:00+00:00,
usageEndTime:2018-01-02T00:00:00+00:00,
meterName:Premium Storage - Snapshots (GB),
meterRegion:AU East,
meterCategory:Storage,
meterSubCategory:Locally Redundant,
unit:GB,
instanceData:
{
"Microsoft.Resources":{
"resourceUri":"/subscriptions/ xxx-xxx-xxx-xxx-xxx /resourceGroups/RGNAME/providers/Microsoft.Storage/storageAccounts/STGNAME",
"location":"aueast",
"tags":{
"Project":"XXX",
"costCentre":"1234"
}
}
},
meterId:b74c1bd6-c0ea-4248-b00a-dfe2afce7af0,
infoFields:[:],
quantity:0.005514
答案 0 :(得分:1)
发生这种情况的原因是json.value [i] .properties.instanceData是 String 的实例,如错误消息中所述。
您需要将此字符串解析为json。
def parsedJson = new groovy.json.JsonSlurper().parseText(json.value[i].properties.instanceData)
println parsedJson['Microsoft.Resources']