使用 Unirest 和 Java 以及 Selenium WebDriver 解析 Json

Parse Json with Unirest and Java and Selenium WebDriver

我正在测试本地 api。 使用下面的代码,我得到了一个响应体。 我需要从 "returnMessage""returnCode""merchantOrderId" 获取数据,我试过很多方法都做不到,有人能帮我吗?

Api 测试代码:

@Test 
        public void Cod04Autorized() {


            HttpResponse<String> response = Unirest.post("http://localhost:8080/api/cielo/pagamentos/transacaoCompleta")
              .header("Content-Type", "application/json")
              .body("{\r\n\t\"merchantOrderId\": \"11234099\",\r\n\t\"client\": {\r\n\t\t\"name\": \"Joao das Neves Teste No Cap\"\r\n\t},\r\n\t\"payment\": {\r\n\t\t\"amount\": 10000,\r\n\t\t\"installments\": 1,\r\n\t\t\"capture\": false,\r\n\t\t\"card\": {\r\n\t\t\t\t\"cardNumber\": \"0000.0000.0000.0001\",\r\n\t\t\t\t\"holderName\": \"Joao das Neves\",\r\n\t\t\t\t\"expirationDate\": \"12/2030\",\r\n\t\t\t\t\"securityCode\": \"123\",\r\n\t\t\t\t\"brand\" : \"Visa\"\r\n\t\t}\r\n\t}\r\n}")
              .asString();


            Assert.assertEquals(200, response.getStatus());                 


            }

JSON 响应:

{
   "error":null,
   "content":{
      "merchantOrderId":"11234099",
      "customer":{
         "name":"Joao das Neves Teste No Cap",
         "email":null,
         "birthDate":null,
         "identity":null,
         "identityType":null,
         "address":null,
         "deliveryAddress":null
      },
      "payment":{
         "serviceTaxAmount":0,
         "installments":1,
         "interest":"0",
         "capture":false,
         "authenticate":false,
         "recurrent":false,
         "recurrentPayment":null,
         "creditCard":{
            "cardNumber":"0000.0*********0001",
            "holder":"Joao das Neves",
            "expirationDate":"12/2030",
            "securityCode":null,
            "saveCard":false,
            "brand":"Visa",
            "cardToken":null
         },
         "debitCard":null,
         "tid":"1219040857754",
         "proofOfSale":"857754",
         "authorizationCode":"634929",
         "softDescriptor":"123456789ABCD",
         "returnUrl":null,
         "provider":"Simulado",
         "paymentId":"7d263fbc-01cf-4fa8-a81d-130bfd1190a8",
         "type":"CreditCard",
         "amount":10000,
         "receivedDate":"2019-12-19 16:08:56",
         "capturedAmount":null,
         "capturedDate":null,
         "currency":"BRL",
         "country":"BRA",
         "returnCode":"4",
         "returnMessage":"Operation Successful",
         "status":1,
         "links":[
            {
               "Method":"GET",
               "Rel":"self",
               "Href":"https://apiquerysandbox.cieloecommerce.cielo.com.br/1/sales/7d263fbc-01cf-4fa8-a81d-130bfd1190a8"
            },
            {
               "Method":"PUT",
               "Rel":"capture",
               "Href":"https://apisandbox.cieloecommerce.cielo.com.br/1/sales/7d263fbc-01cf-4fa8-a81d-130bfd1190a8/capture"
            },
            {
               "Method":"PUT",
               "Rel":"void",
               "Href":"https://apisandbox.cieloecommerce.cielo.com.br/1/sales/7d263fbc-01cf-4fa8-a81d-130bfd1190a8/void"
            }
         ],
         "extraDataCollection":null,
         "expirationDate":null,
         "url":null,
         "number":null,
         "barCodeNumber":null,
         "digitableLine":null,
         "address":null,
         "boletoNumber":null,
         "demonstrative":null,
         "identification":null,
         "instructions":null,
         "authenticationUrl":null
      }
   }
}

如果您只想获取returnMessagereturnCodemerchantOrderId这3个字段,可以使用Jayway JsonPath.

轻松完成

Maven 依赖

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

代码片段

DocumentContext jsonContext = JsonPath.parse(jsonResponse);
String returnMessage = jsonContext.read("$.content.payment.returnMessage");
String returnCode = jsonContext.read("$.content.payment.returnCode");
String merchantOrderId = jsonContext.read("$.content.merchantOrderId");