Karate 如何在空手道中将JSON值与响应值匹配

Karate 如何在空手道中将JSON值与响应值匹配,karate,Karate,我想将JSON值与空手道中的响应值相匹配。 如何从JSON和Response中检索所有字段值并进行匹配? 任何解决方案都值得赞赏 下面是示例JSON和响应 JSON: { "Field1": 123, " Field2": 456, " Field3": "O", " Field4": 1000 }, { "Field1": 678, " Field2": 234, " Field3": "P", " Field4": 20

我想将JSON值与空手道中的响应值相匹配。 如何从JSON和Response中检索所有字段值并进行匹配? 任何解决方案都值得赞赏 下面是示例JSON和响应

 JSON: {
    "Field1": 123,
    " Field2": 456,
    " Field3": "O",
    " Field4": 1000
  },
  {    "Field1": 678,
    " Field2": 234,
    " Field3": "P",
    " Field4": 2000
  }
]

Response:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Header/>
   <S:Body>
      <ns12: Response>
         <ns12: ResponseList>
            <ns7: Field1>123</ns7: Field1>
            <ns7: Field2>456</ns7: Field2>
            <ns7: Field3>O</ns7: Field3>
            <ns7: Field4>1000</ns7: Field4>

         </ns12: ResponseList >
         <ns12: ResponseList >
            <ns7: Field1>678</ns7: Field1>
            <ns7: Field2>234</ns7: Field2>
            <ns7: Field3>P</ns7: Field3>
            <ns7: Field4>2000</ns7: Field4>
         </ns12: ResponseList >
      </ns12: Response >
   </S:Body>
</S:Envelope>
JSON:{
“字段1”:123,
“字段2”:456,
“字段3”:“O”,
“字段4”:1000
},
{“字段1”:678,
“字段2”:234,
“字段3”:“P”,
“领域4”:2000年
}
]
答复:
123
456
O
1000
678
234
P
2000

有关其工作原理的大致说明,请参见

我想给你举一个例子

4.2。测试响应 让我们编写另一个测试REST端点是否返回特定响应的场景:

Scenario: Testing the exact response of a GET endpoint
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ == {id:"1234",name:"John Smith"}
The match operation is used for the validation where ‘$' represents the response. So the above scenario checks that the response exactly matches ‘{id:”1234″,name:”John Smith”}'.
我们还可以专门检查id字段的值:

And match $.id == "1234"
The match operation can also be used to check if the response contains certain fields. This is helpful when only certain fields need to be checked or when not all response fields are known:

Scenario: Testing that GET response contains specific field
Given url 'http://localhost:8080/user/get'
When method GET
Then status 200
And match $ contains {id:"1234"}
另一个例子来自

让我们看一下测试POST端点并获取请求正文的最后一个场景:

  Scenario: Create and retrieve a Product
    Given path 'products'
    And request { "name": "My product", "type": "Super Type", "price": 123, "shipping": 0, "upc": "041345324016", "description": "My super nice aweome product", "manufacturer": "Feo Hero", "model": "QB2400B4Z", "url": "some.url", "image": "some.image" }
    When method POST
    Then status 201
    And def product = response

    Given path '/products/'+product.id
    When method GET
    Then status 200
    And match $ contains {id:'#(product.id)',name:'#(product.name)',type:'#(product.type)',price:#(product.price)}
Soap有点不同,示例如下:


可能的副本非常感谢彼得证明了有用的链接。这真的帮了大忙。但是,在实施您的解决方案后,我仍然存在一些问题。1.我得到一个错误,实际值不存在。空手道中的键是否区分大小写?2.如何根据下面的示例匹配不同的日期和金额格式?感谢您的帮助,“OTHERNO”:40001}不,请阅读文档并提出具体问题
Given request read('soap-request.xml')
When soap action 'QueryUsageBalance'
Then status 200
And match response /Envelope/Body/QueryUsageBalanceResponse/Result/Error/Code == 'DAT_USAGE_1003'
And match response /Envelope/Body/QueryUsageBalanceResponse == read('expected-response.xml')