有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用java验证Json响应

我想从下面的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) 个答案

  1. # 1 楼答案

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

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

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

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