【问题标题】:How to validate json unnamed array in karate如何在空手道中验证 json 未命名数组
【发布时间】:2018-07-30 13:20:07
【问题描述】:

我正在尝试验证以下响应,它是一个没有数组名称的数组。

[
    {
        "soldTo": "0000100139",
        "productId": "88175838",
        "catalogNumber": "130C-3/4X30FT",
        "manufacturerName": "3M CO. - ELECTRICAL MARKETS DIV.",
        "quantity": "000001",
        "lastPurchaseDate": "Jun 13, 2018 12:00:00 AM",
        "description": "30-feet x 3/4-inch black Scotch&#174 linerless rubber splicing tape.",
        "imageUrl": "https://static.graybar.com/supplierimages/3m_new/130C-1-12X30FT-th.png"
    },
    {
        "soldTo": "0000100139",
        "productId": "25438652",
        "catalogNumber": "312-POUCH",
        "manufacturerName": "3M CO. - ELECTRICAL MARKETS DIV.",
        "quantity": "000011",
        "lastPurchaseDate": "Jul 27, 2018 12:00:00 AM",
        "description": "Yellow Scotchlok&#8482 electrical spring connector in a pouch for 22 to 8 AWG wire.",
        "imageUrl": "https://static.graybar.com/supplierimages/3m_new/312-BAG-th.png"
    },
    {
        "soldTo": "0000100139",
        "productId": "88175944",
        "catalogNumber": "33+SUPER-3/4X66FT",
        "manufacturerName": "3M CO. - ELECTRICAL MARKETS DIV.",
        "quantity": "000001",
        "lastPurchaseDate": "Jun 13, 2018 12:00:00 AM",
        "description": "66-feet x 3/4-inch black Scotch&#174 Super 33+&#8482 vinyl electrical tape.",
        "imageUrl": "https://static.graybar.com/supplierimages/3M_New/MMM_33Super34x66FT-th.png"
    }
]

当我尝试使用以下匹配每个脚本时出现错误。

And match each response.soldTo == '#string'
    And match each response.productId == '#string'
    And match each response.catalogNumber == '#string'
    And match each response.manufacturerName == '#string'
    And match each response.quantity == '#string'
    And match each response.lastPurchaseDate == '#string'
    And match each response.description == '#string'
    And match each response.imageUrl == '#string

'

错误: com.intuit.karate.exception.KarateException:应在路径 $ 中找到具有属性 ['soldTo'] 的对象,但找到了 'net.minidev.json.JSONArray'。这不是根据 JsonProvider 的 json 对象:'com.jayway.jsonpath.spi.json.JsonSmartJsonProvider'。 在 com.intuit.karate.StepDefs.matchNamed(StepDefs.java:540) 在 com.intuit.karate.StepDefs.matchEquals(StepDefs.java:526) 在✽。并匹配每个 response.soldTo == "0000100139"(previously-order-items/previously-order-items.feature:24)

【问题讨论】:

  • 使用下面的脚本得到另一个错误:并匹配每个响应 == ' """ { soldTo: '#notnull', productId: '#string', catalogNumber: '#string', manufacturerName: ' #string', 数量: '#string', lastPurchaseDate: '#string' 描述: '#string' imageUrl: '#string' } """ ' Error: com.intuit.karate.exception .KarateException: net.minidev.json.parser.ParseException: 位置 166 处的意外标记 d。
  • 您能否将此答案标记为已接受:stackoverflow.com/a/51596405/143475

标签: karate


【解决方案1】:

您在 JsonPath 中犯了一个错误。请仔细阅读文档和示例。

* match each response[*].soldTo == '#string'
* match each response[*] contains { productId: '#string', catalogNumber: '#string' }

【讨论】:

  • 使用 * 匹配每个响应 [] 包含 { productId: '#string', catalogNumber: '#string' } 得到相同的 com.intuit.karate.exception.KarateException: net.minidev .json.parser.ParseException:在 com.intuit.karate.StepDefs.matchContains(StepDefs.java:532) 的 com.intuit.karate.StepDefs.matchNamed(StepDefs.java:540) 的位置 166 处出现意外标记 d .intuit.karate.StepDefs.matchContainsDocString(StepDefs.java:519) at ✽. 匹配每个响应[*] contains(previously-order-items/previously-order-items.feature:25)
  • @ManojKumarKotturi 好的,我放弃了。这个对我有用。祝一切顺利 ! github.com/intuit/karate/wiki/How-to-Submit-an-Issue
  • 谢谢! * 匹配每个响应[*].soldTo == '#string' 这对我有用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 2019-09-05
相关资源
最近更新 更多