【问题标题】:converting a string that contains an array into a json object将包含数组的字符串转换为 json 对象
【发布时间】: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 确实对结果进行了字符串化......当它从请求中返回一个值时,如果你想从另一个测试用例中使用它,你只能以字符串格式进入(除非有技巧我不知道吗?)。有时这确实是个问题:-/

标签: arrays json groovy soapui


【解决方案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}

该字符串中有不匹配的大括号。有 6 个{ 和 5 个}。解析器无法处理。

编辑

2 其他问题是键没有被引用并且有= 应该有:。还有像 ... , dynamic_href/properties/triggers, ... 这样的东西不是有效的 JSON。

【讨论】:

  • 也没有引用键。那样可以么?我实际上并没有尝试过...我总是看到像{'hello': 1} 这样的东西,但我想我不知道它是否需要。
  • @Daniel 有一个“宽松”模式,如果启用,它将允许这样做。并不是说它在这里会有所帮助......
  • @cfrick 很有趣,感谢您提供的信息!很高兴“松弛”还不足以忽略丢失的大括号...... :-)
  • 好吧,我以为没有中间处理,但实际上有一些。你是对的,内容缺少一些大括号!我再次完成了所有过程并设法纠正了它,所以现在,我有一个正确的输出格式,并且 json 解析按预期工作。谢谢!
  • @A.Joly "...现在,我的输出格式正确,json 解析按预期工作" - 太棒了!我很高兴你解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-29
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 2023-02-06
  • 2012-05-08
相关资源
最近更新 更多