【问题标题】:How to parse below JSON response using Karate API如何使用空手道 API 解析以下 JSON 响应
【发布时间】:2019-02-17 17:52:36
【问题描述】:

任何人都可以了解如何在空手道 API 中拆分以下响应

这里我想从响应中提取 partNumber 和 productTitle,其中响应中的初始数字是动态的,对于每个 get 方法,数字都会改变。

{"items":{'41651625424': {itemCore: {partNumber: '1234567', productTitle: 'Karate API Testing'}}}}

【问题讨论】:

    标签: karate


    【解决方案1】:

    有多种方法,但在这种情况下,对我来说最好的选择是使用 JsonPath:

    * def response = { "items": { '41651625424': { itemCore: { partNumber: '1234567', productTitle: 'Karate API Testing' }}}}
    * def itemCore = get[0] response..itemCore
    * match itemCore == { partNumber: '1234567', productTitle: 'Karate API Testing' }
    

    参考文档:https://github.com/intuit/karate#get-plus-index

    【讨论】:

    • 嗨彼得,感谢您的快速回复。我可以得到带有星号的 partNumber 和 productTtile。但是我想在一个变量中检索 41651625424 这个值,因为我必须将它作为另一个请求正文中的输入传递
    • 嗨彼得,下面的语句在 eclipse 中运行良好,但在 intelliJ 中运行良好,def itemCore = get[0] response..itemCore getting java.lang.RuntimeException: javascript evaluation failed: get[0] response ..itemCore
    • @sam 是时候让我指出这一点了:github.com/intuit/karate/wiki/How-to-Submit-an-Issue
    • 如何从下面的响应中获取第二个零件号。我试过 get[1] 但它打印的是空的。 {“wasAdded”:真,“lineItems”:{“1111111”:{“itemCore”:{“partNumber”:“10000061”}},“222222”:{“itemCore”:{“partNumber”:“10000062”} }, "3333333": { "itemCore": { "partNumber": "10000063" } } } }
    • @sam 请打开一个新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2019-12-13
    • 1970-01-01
    • 2019-12-11
    • 2019-12-19
    相关资源
    最近更新 更多