【发布时间】:2020-05-17 14:29:41
【问题描述】:
我是 Groovy 的新手,但使用它从存储在文件中的 JSON 响应中提取响应。
以下是 JSON 的 sn-p:
"attachments": [
{
"type": "AttachmentText",
"text": "Line 1"
},
{
"type": "AttachmentText",
"text": "This is a different text and can be anything but always > 8 characters"
}
],
我正在尝试根据first case is always < 8 字符中的文本而second case is always >8 字符中的文本的条件获取文本 - 没有其他方法可以区分附件元素。但是我的代码只给了我 1 个响应。
{
def jsonSlurper = new JsonSlurper()
def object = jsonSlurper.parseText(jsontocsv.toString())
def array1 = object.attachments
if(array1 != null && !array1.isEmpty())
{
for(def i =0; i<object.attachments.size();i++)
{
if(object.attachments[i].type=="AttachmentText" && object.attachments[i].text.length()>8) {
varaiable1 = RString.of(object.attachments[i].text.toString())
}
else{
variable2 = RString.of("Nothing here")
}
}
}
else {
variable3 = RString.of("No attachments")
}
}
我希望我的variable1 会显示响应这是一个不同的文本,可以是任何内容,但总是 > 8 个字符,但我不断收到 这里没有 .
知道如何解决这个问题吗?
【问题讨论】:
-
您的
variable2从分配的JSON 中获取最后一个值,这似乎不合适。添加一些调试信息以检查 -
上面的代码不可能将
variable1设置为“这里没有”...它只能将variable2设置为...有没有理由你有3个不同的变量名?他们不应该都是variable1吗?另请注意variable1拼写错误。最后,尝试添加一个“break;”分配变量 1 后的语句,以便在找到所需内容时停止循环。
标签: json groovy rpa workfusion