[英]Having trouble accessing fields of JSON using groovy
I'm trying to extract the information from an API response using groovy 我正在尝试使用groovy从API响应中提取信息
I want to get to the info stored under the "res" key. 我想获取存储在“ res”键下的信息。
Something along the lines of body.measures.02:00:00:02:06:70.res 身体上的东西.measures.02:00:00:02:06:70.res
Can not figure out have to access this information without throwing a nullpointerexception. 无法弄清楚必须在不抛出nullpointerexception的情况下访问此信息。
"body":[ {
"_id":"70:ee:50:01:fe:96",
"place":{
"location":[
-70.863189,
42.273936
],
"altitude":26.154942,
"timezone":"America\/New_York"
},
"mark":12,
"measures":{
"02:00:00:02:06:70":{
"res":{
"1506611038":[
22,
66
]
},
"type":[
"temperature",
"humidity"
]
},
"06:00:00:01:97:28":{
"wind_strength":15,
"wind_angle":343,
"gust_strength":29,
"gust_angle":301,
"wind_timeutc":1506611083
},
"70:ee:50:01:fe:96":{
"res":{
"1506611086":[
1007.4
]
},
"type":[
"pressure"
]
}
},
"modules":[
"02:00:00:02:06:70",
"06:00:00:01:97:28"
],
"module_types":{
"02:00:00:02:06:70":"NAModule1",
"06:00:00:01:97:28":"NAModule2"
}
}
],
"status":"ok",
"time_exec":0.017483949661255,
"time_server":1506611446
}
body
is an array with one element in your case, so you have to do body[0].measures.'02:00:00:02:06:70'.res
, eg like body
是一个包含一个元素的数组,因此您必须执行body[0].measures.'02:00:00:02:06:70'.res
,例如
new groovy.json.JsonSlurper().parseText('''{"body":[ {
"_id":"70:ee:50:01:fe:96",
"place":{
"location":[
-70.863189,
42.273936
],
"altitude":26.154942,
"timezone":"America/New_York"
},
"mark":12,
"measures":{
"02:00:00:02:06:70":{
"res":{
"1506611038":[
22,
66
]
},
"type":[
"temperature",
"humidity"
]
},
"06:00:00:01:97:28":{
"wind_strength":15,
"wind_angle":343,
"gust_strength":29,
"gust_angle":301,
"wind_timeutc":1506611083
},
"70:ee:50:01:fe:96":{
"res":{
"1506611086":[
1007.4
]
},
"type":[
"pressure"
]
}
},
"modules":[
"02:00:00:02:06:70",
"06:00:00:01:97:28"
],
"module_types":{
"02:00:00:02:06:70":"NAModule1",
"06:00:00:01:97:28":"NAModule2"
}
}
],
"status":"ok",
"time_exec":0.017483949661255,
"time_server":1506611446
}}''').body[0].measures.'02:00:00:02:06:70'.res
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.