空手道中的 PolyglotException,当某些 json 键具有连字符“-”时

问题描述 投票:0回答:1

当 json 对象中的键包含连字符时,可能会导致空手道出现问题。这是一个例子 -

{
  "id": "1234",
  "type": "FICTION",
  "attribute": {
    "bookPrice": "10",
    "authorName": {
      "firstName": "Dean",
      "lastName": "Jones"
    },
    "links": {
      "get-book-by-id": {
        "href": "/v1/books/1234",
        "title": "get-book-by-id"
      }
    }
  }
}

在此示例中,键“get-book-by-id”由连字符分隔,当我尝试遍历此元素时,我收到如下错误 -

org.graalvm.polyglot.PolyglotException: ReferenceError: 'book' is not defined.
json karate
1个回答
0
投票
Feature: Sample

  Scenario: Test1
    * def response =
      """
      {
        "id": "1234",
        "type": "FICTION",
        "attribute": {
          "bookPrice": "10",
          "authorName": {
            "firstName": "Dean",
            "lastName": "Jones"
          },
          "links": {
            "get-book-by-id": {
              "href": "/v1/books/1234",
              "title": "get-book-by-id"
            }
          }
        }
      }
      """
    
    * print response.attribute.links["get-book-by-id"]
© www.soinside.com 2019 - 2024. All rights reserved.