Best way to code this, string to map conversion in Groovy

Posted by Daxon on Stack Overflow See other posts from Stack Overflow or by Daxon
Published on 2010-05-11T16:50:00Z Indexed on 2010/05/11 16:54 UTC
Read the original article Hit count: 271

Filed under:
|
|
|

I have a string like

def data = "session=234567893egshdjchasd&userId=12345673456&timeOut=1800000"

I want to convert it to a map

 ["session", 234567893egshdjchasd]
 ["userId", 12345673456]
 ["timeout", 1800000]

This is the current way I am doing it,

 def map = [:]

 data.splitEachLine("&"){

   it.each{ x ->

     def object = x.split("=")
     map.put(object[0], object[1])

   }

 }

It works, but is there a more efficient way?

© Stack Overflow or respective owner

Related posts about groovy

Related posts about string