1

如果我有一个包含特殊字符(在我的情况下为点)字段的 json 文件,我如何访问空手道中的字段值?

例如有一个名为 example.json 的 json 文件

{
  "field1" : {
      "field2" : "value2",
      "field.3" : "value3"
  }
}

如果我想获得“field.3”字段的值怎么办?

  Scenario: read a json file
    * def myJson = read("example.json")
    * match myJson.field1.field2 == "value2"
    * match myJson.field1.field.3 == "value3" # this fails
    * match myJson.field1."field.3" == "value3" # this fails
    * match myJson.field1.'field.3' == "value3" # this fails
    * match myJson.field1.'field\.3' == "value3" # this fails
4

1 回答 1

1

使用方括号:

* myJson.field1['field.3']
于 2020-06-16T11:31:37.557 回答