Groovy - ArrayList 删除括号
Groovy - ArrayList remove brackets
我已经解析了下面的 Json 文件并检索了用户名值。
"LogInFunctionTest": [
{
"TestCaseID": "Login-TC_02",
"TestScenario": "Negative Case - Login with unregistered username and Password",
"TestData": [
{
"UserName": "usernameX",
"Password": "passwordX"
}
]
使用以下代码检索 UserName
值。
def InputJSON = new JsonSlurper().parse(new File(fileName))
def testDataItem = InputJSON.LogInFunctionTest.find { it.TestCaseID == Login-TC_02 }.TestData.UserName
输出 - [usernameX]
在此之后,我想删除上面的括号。
'Remove brackets from arraylist'
testDataItem = testDataItem.substring(1, testDataItem.length() - 1)
我遇到以下异常
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.length() is applicable for argument types: () values: []
Possible solutions: last(), last(), init(), init(), get(int), get(int)
有人指导我们如何从输出中删除括号吗?
testDataItem = testDataItem.get(0)
可能会胜任。
看起来您正在读取字符串列表,而不是字符串。
testDataItem
是一个用户名列表,因为 TestData
包含一个列表
这就是为什么当它显示给您时,它有 []
圆...
如果你只想要列表中的第一个,那么你可以这样做:
def testDataItem = InputJSON
.LogInFunctionTest
.find { it.TestCaseID == 'Login-TC_02' }
.TestData
.UserName
.first()
(即:在最后调用 first()
)
显然,如果有两个,您只会得到第一个
您也可以使用 .UserName[0]
获得第一个,但 .first()
更具描述性
从技术上讲,您将 'TestData' 称为带有上面括号的列表,因此正确的引用应该是:
TestData[0].UserName
... 或者因为它全部包含在 'LogInFunctionTest' 列表中:
LogInFunctionTest[0].TestData[0].UserName
也就是说,如果您不打算 loop/iterate 通过它们。
我已经解析了下面的 Json 文件并检索了用户名值。
"LogInFunctionTest": [
{
"TestCaseID": "Login-TC_02",
"TestScenario": "Negative Case - Login with unregistered username and Password",
"TestData": [
{
"UserName": "usernameX",
"Password": "passwordX"
}
]
使用以下代码检索 UserName
值。
def InputJSON = new JsonSlurper().parse(new File(fileName))
def testDataItem = InputJSON.LogInFunctionTest.find { it.TestCaseID == Login-TC_02 }.TestData.UserName
输出 - [usernameX]
在此之后,我想删除上面的括号。
'Remove brackets from arraylist'
testDataItem = testDataItem.substring(1, testDataItem.length() - 1)
我遇到以下异常
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList.length() is applicable for argument types: () values: []
Possible solutions: last(), last(), init(), init(), get(int), get(int)
有人指导我们如何从输出中删除括号吗?
testDataItem = testDataItem.get(0)
可能会胜任。
看起来您正在读取字符串列表,而不是字符串。
testDataItem
是一个用户名列表,因为 TestData
包含一个列表
这就是为什么当它显示给您时,它有 []
圆...
如果你只想要列表中的第一个,那么你可以这样做:
def testDataItem = InputJSON
.LogInFunctionTest
.find { it.TestCaseID == 'Login-TC_02' }
.TestData
.UserName
.first()
(即:在最后调用 first()
)
显然,如果有两个,您只会得到第一个
您也可以使用 .UserName[0]
获得第一个,但 .first()
更具描述性
从技术上讲,您将 'TestData' 称为带有上面括号的列表,因此正确的引用应该是:
TestData[0].UserName
... 或者因为它全部包含在 'LogInFunctionTest' 列表中:
LogInFunctionTest[0].TestData[0].UserName
也就是说,如果您不打算 loop/iterate 通过它们。