简体   繁体   中英

Having trouble accessing fields of JSON using groovy

I'm trying to extract the information from an API response using groovy

I want to get to the info stored under the "res" key.

Something along the lines of body.measures.02:00:00:02:06:70.res

Can not figure out have to access this information without throwing a 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM