Reading a JSON key that has a hyphen in it?

I've come across a Json file that contains a key that has a hypen in it. I've done tons of searching on Google but can't figure out how to read this.

Example Json:
"country_name":"United States",
"iso-3166":"US",
etc...

I can read the 'counry_name' just fine. But the 'iso-3166' is throwing an error because it thinks it's a minus sign.

Here's an example of the code I'm using... I've tried many ways of getting the iso value with no luck. This is just the latest attempt. :upside_down_face:

httpGet(params) { resp ->
  def json = resp.data
   for (rec in json.response.countries) {
          cName += "$rec.country_name ($rec.['iso-3166'])"
   } 
}

Desired output is 'United States (US)'

Appreciate any help!

Shot in the dark here. Have you tried double-quotes around iso-3166 instead of single-quotes?

So [β€œiso-3166”]?

I got it!

       httpGet(params) { resp ->
            def json = resp.data
            for (rec in json.response.countries) {
                iso = rec['iso-3166']
                cName += "$rec.country_name ($iso)"
            } 
        }

I was soo close! lol

2 Likes

Another shot in dark…

Was about to propose something similar and see you got it.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.