【发布时间】:2021-05-04 15:12:32
【问题描述】:
在 SOAPUI 中,请求以字符串的形式返回值,无论它们包含什么。 请求在时间 t 返回以下字符串:
{satisfied=true, dynamic_href/properties/triggers, name=triggers, value={satisfied=true, dynamic_href/properties/triggers, name=triggers, value=[{type=DAILY, description=null, configuration={allXDays=1, time=08:10:29, timeProgramMode=TIME, startDate=2019-02-28}, id=guid], metadata={satisfied=true, href=/m2m/fim/metadata/items/com.hager.domovea.automation.sequence.Sequence/properties/triggers, name=null, description=null, type=null, classes=null, regex=null, min=null, max=null, step=null, enumeration=null, beanMetadata=null, readable=null, writable=null, eventable=null, asyncStatusPropertyName=null, attributes=null}, asyncStatusProperty=null}, metadata={satisfied=true, href=/m2m/fim/metadata/items/com.hager.domovea.automation.sequence.Sequence/properties/triggers, name=null, description=null, type=null, classes=null, regex=null, min=null, max=null, step=null, enumeration=null, beanMetadata=null, readable=null, writable=null, eventable=null, asyncStatusPropertyName=null, attributes=null}, asyncStatusProperty=null}
在时间 t+1 相同的请求返回以下内容
{satisfied=true, dynamic_href/properties/triggers, name=triggers, value={satisfied=true, dynamic_href/properties/triggers, name=triggers, value=[{type=DAILY, description=null, configuration={allXDays=1, timeProgramMode=TIME, startDate=2019-02-28, time=08:10:29}, id=guid], metadata={satisfied=true, href=/m2m/fim/metadata/items/com.hager.domovea.automation.sequence.Sequence/properties/triggers, name=null, description=null, type=null, classes=null, regex=null, min=null, max=null, step=null, enumeration=null, beanMetadata=null, readable=null, writable=null, eventable=null, asyncStatusPropertyName=null, attributes=null}, asyncStatusProperty=null}, metadata={satisfied=true, href=/m2m/fim/metadata/items/com.hager.domovea.automation.sequence.Sequence/properties/triggers, name=null, description=null, type=null, classes=null, regex=null, min=null, max=null, step=null, enumeration=null, beanMetadata=null, readable=null, writable=null, eventable=null, asyncStatusPropertyName=null, attributes=null}, asyncStatusProperty=null}
我必须比较它们以检查是否没有属性更改,但是值数组中存在细微的顺序变化,因此无法直接进行字符串比较。 当我尝试使用 jsonSlurper 对其进行转换以便可以逐个元素进行比较时,它会引发错误。 我认为它可以帮助我解析我的回复并轻松进行比较。
我正在尝试找到一种方法来解析高级属性(例如“满足”)以及嵌套属性(例如值或元数据)以及更低级别的数组...
编辑
尝试使用 json slurper 解析时出现以下错误:
expecting '}' or ',' but got current char 's' with an int value of 115
The current character read is 's' with an int value of 115
expecting '}' or ',' but got current char 's' with an int value of 115
line number 1
index number 1
{satisfied=true, dynamic_href/properties/triggers, name=triggers, value={satisfied=true, dynamic_href/properties/triggers, name=triggers, value=[{type=DAILY, description=mon trigger, configuration={allXDays=1, action=switchOn, time=16:01:02, timeProgramMode=TIME, startDate=2018-04-11}, id=guid], metadata={satisfied=true, href=/m2m/fim/metadata/items/com, hager, domovea, automation, homestatus, HomeStatus/properties/triggers, name=null, description=null, type=null, classes=null, regex=null, min=null, max=null, step=null, enumeration=null, beanMetadata=null, readable=null, writable=null, eventable=null, asyncStatusPropertyName=null, attributes=null}, asyncStatusProperty=null}, metadata={satisfied=true, href=/m2m/fim/metadata/items/com, hager, domovea, automation, homestatus, HomeStatus/properties/triggers, name=null, description=null, type=null, classes=null, regex=null, min=null, max=null, step=null, enumeration=null, beanMetadata=null, readable=null, writable=null, eventable=null, asyncStatusPropertyName=null, attributes=null}, asyncStatusProperty=null}
.^
【问题讨论】:
-
“当我尝试使用 jsonSlurper 转换它以便我可以逐个元素进行比较时,它会引发错误” - 如果您展示了您尝试过的内容并分享了错误内容,将会很有帮助提出。
-
在最近编辑显示的 JSON 中,有 6 个左大括号 (
{),只有 5 个右大括号 (})。 -
JSON slurper 抱怨,因为它被要求解析无效的 JSON。
-
您想要比较的东西很可能不是字符串,而是类似的东西将它变成了字符串(例如
.toString())——甚至可能是您自己的代码(检查@987654327 @你的数据)。您最好防止字符串化(因为它通常不是正确的序列化格式)并处理实际对象。如果你的上游服务器真的只返回一个字符串,那么看看服务器发送的内容类型。然后使用可以读取该格式的库。 -
当然,ReadyAPI 确实对结果进行了字符串化......当它从请求中返回一个值时,如果你想从另一个测试用例中使用它,你只能以字符串格式进入(除非有技巧我不知道吗?)。有时这确实是个问题:-/