简体   繁体   English

ArrayList 字符串 - 使用 Groovy 检索值

[英]ArrayList string - Retrieve Values using Groovy

Parsing the value from JSON and stored it into a variable using Groovy.解析 JSON 中的值并使用 Groovy 将其存储到变量中。 below is the value stored in that variable.下面是存储在该变量中的值。

[[UserName:usernameX, Password:passwordX]]

The format is Arraylist, how can I retrieve the UserName and Password value like [ usernameX and passwordX ].格式为 Arraylist,如何检索 UserName 和 Password 值,如 [ usernameXpasswordX ]。

Hmmm, double [[]] and no quotes.嗯,双 [[]] 并且没有引号。 I don't know how your data gets into an ArrayList, but if you can get the data in a Groovy Map, you can do it like this:我不知道您的数据如何进入 ArrayList,但如果您可以在 Groovy Map 中获取数据,您可以这样做:

Map a = ['UserName':'usernameX', 'Password':'passwordX']

a.each { k, v ->
    println v
}

prints:印刷:

usernameX
passwordX

You can convert the nested array-list straight into a map:您可以将嵌套数组列表直接转换为 map:

Map map = [[UserName:'usernameX', Password:'passwordX']].collectEntries{ [ it.UserName, it.Password ] }

assert map == [usernameX:'passwordX']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM