使用java验证Json响应

时间:2018-05-04 12:48:27

标签: java json rest rest-assured

我想从下面的Jason回复中获取textType和taxAmount

{
"taxExempt": false,
"billingAddress": {
    "addressLine1": "Millers Rd",
    "addressLine2": "",
    "city": "Bengaluru",
    "postalCode": "560052",
    "state": "KA",
    "country": "IN",
    "name": null,
    "email": null
},
"shippingAddress": null,
"billingAccountKey": "20000052",
"invoiceKey": null,
"date": "2018-01-01",
"currency": "AUD",
"taxNumber": "AACCL1660QSD002",
"aggregateTax": true,
"totalTax": 0,
"items": [
    {
        "taxes": [
            {
                "taxType": "GST_IN",
                "taxAmount": 0,
                "taxRate": 0
            }
        ],
        "productFamily": "OpenVoice",
        "productKey": "HDEFSS",
        "referenceProductKey": null,
        "taxCode": "C655",
        "quantity": 1,
        "unitPrice": 100,
        "extendedPrice": 100,
        "overrideTaxAmount": null,
        "LMIEntity": "LogMeIn US"
    },
    {
        "taxes": [
            {
                "taxType": "GST_IN",
                "taxAmount": 0,
                "taxRate": 0
            }
        ],
        "productFamily": "OpenVoice",
        "productKey": "ITFS",
        "referenceProductKey": null,
        "taxCode": "C655",
        "quantity": 1,
        "unitPrice": 100,
        "extendedPrice": 100,
        "overrideTaxAmount": null,
        "LMIEntity": "LogMeIn US"
    },
    {
        "taxes": [
            {
                "taxType": "GST_IN",
                "taxAmount": 0,
                "taxRate": 0
            }
        ],
        "productFamily": "GoToMeeting",
        "productKey": "G2M_Pro",
        "referenceProductKey": null,
        "taxCode": "C910",
        "quantity": 20,
        "unitPrice": 49,
        "extendedPrice": 980,
        "overrideTaxAmount": null,
        "LMIEntity": "LogMeIn US"
    }
]
}

1 个答案:

答案 0 :(得分:1)

 ObjectMapper mapper = new ObjectMapper(); 
 JsonNode jsonObj = mapper.readTree(plain_jsondata);
 String totalTax= jsonObj.get("totalTax");

创建一个JsonNode对象并使用.get("key")来读取值。

这两个类都是Jackson lib (com.fasterxml.jackson)的一部分。

对于嵌套项,首先读取并存储父项,然后在父项上执行相同操作。