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

后端 未结 8 2001
情深已故
情深已故 2020-12-31 07:22

I have a string like

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

I want to convert it to a map

8条回答
  •  Happy的楠姐
    2020-12-31 08:01

    I don't know if this is more efficient, but to my eyes, it's a bit simpler (YMMV)

    def data = "session=234567893egshdjchasd&userId=12345673456&timeOut=1800000"
    def map = [:]
    
    data.split("&").each {param ->
        def nameAndValue = param.split("=")
        map[nameAndValue[0]] = nameAndValue[1]
    }
    

提交回复
热议问题