Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access map values in groovy by key containing dot?

Tags:

map

groovy

I have map like this:

data = {user.name: "John",
        user.surname: "Doe",
        city: "NY"}

I can access attiribute 'city' this way:

data.city

Is there is a similar way to access 'user.name' attribute?

like image 783
Vladimir Avatar asked Apr 20 '11 12:04

Vladimir


People also ask

How do I iterate a map in Groovy?

If you wanted to do it that way, iterate over map. keySet() and the rest will work as you expected. It should work if you use s. key & s.

How do I add a key value pair to a map in Groovy?

Add Item to a Map The first way is using the square brackets for the key. This way useful if you have a dynamic key name for example the key name join with index. The second way is using a key separate with map name by a dot ".". Or this example also working in Groovy.

How do I use Groovy maps?

Maps are generally used for storing key-value pairs in programming languages. You have two options to declare a map in groovy. First option is, define an empty map and put key-value pairs after. Second option is declaring map with default values.

Are Groovy maps ordered?

2. The each Method. In Groovy, maps created with the literal notation are ordered.


1 Answers

Assuming you meant:

data = [ 'user.name':"John", 'user.surname':"Doe", city:"NY" ]

(square braces for the map definition, and quotes round the dotted key names), I believe that

data.'user.name'

should do it

like image 121
tim_yates Avatar answered Oct 26 '22 05:10

tim_yates