【问题标题】:Escape dots in Groovy GPathGroovy GPath 中的转义点
【发布时间】:2017-11-10 01:27:37
【问题描述】:

我在 Java 中使用RestAssured 框架,其文档包含此note

请注意,“json 路径”语法使用 Groovy's GPath 表示法,不要与 Jayway 的 JsonPath 语法混淆。

我需要验证以下 JSON:

"_source": {
    "logSource": {
      "logger.name": "LogbackLogger",
    },
}


_source.logSource.logger.name
_source.logSource.logger.name[0] 这样的选择器不返回任何结果。

我认为这是由于 logger.name 属性中的点。 如果没有进行转义,logger.name 会被解释为好像 namelogger 之下,这是不正确的。

如何正确转义 GPath 中的点字符,以便将logger.name 视为单个属性名称?

谢谢!

【问题讨论】:

    标签: json groovy rest-assured gpath rest-assured-jsonpath


    【解决方案1】:

    你有一个小问题。

    只需将其包裹在 single quote 之间,即 'logger.name' 因为有特殊字符。

    这里是完整的例子:

    def string = """{  "_source": {
           "logSource": {
              "logger.name": "LogbackLogger",
          }
        }
    }"""
    
    def json = new groovy.json.JsonSlurper().parseText(string)
    println json.'_source'.logSource.'logger.name'
    

    【讨论】:

    • 感谢您的回复!我已经将它外推到 Java 案例中,并且解决方案似乎确实是微不足道的。我只需要添加单引号:_source.logSource.'logger.name'[0]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 1970-01-01
    相关资源
    最近更新 更多