I have a string like
def data = \"session=234567893egshdjchasd&userId=12345673456&timeOut=1800000\"
I want to convert it to a map>
Here's my effort, which initializes and fills the map in one go, and avoids the inject method which I personally find hard to follow:-
def map = data.split('&').collectEntries {
def kvp = it.split('=').collect { string ->
string = string.trim()
return string
}
[(kvp[0]): kvp.size() > 1 ? kvp[1] ?: '' : '']
// the null check is almost certainly overkill so you could use :-
// [(kvp[0]): kvp.size() > 1 ? kvp[1] : '']
// this just checks that a value was found and inserts an empty string instead of null
}