简体   繁体   English

如何只解析JSON的一部分?

[英]How to Parse only part of JSON?

I get a json:我得到一个 json:

{
"trackResponse": {
    "shipment": [
        {
            "package": [
                {
                    "trackingNumber": "Y0081040217",
                    "deliveryDate": [
                        {
                            "type": "DEL",
                            "date": "20200817"
                        }
                    ],
                    "deliveryTime": {
                        "startTime": "",
                        "endTime": "145339",
                        "type": "DEL"
                    },
                    "activity": [
                        {
                            "location": {
                                "address": {
                                    "city": "TORONTO",
                                    "stateProvince": "",
                                    "postalCode": "",
                                    "country": "CA"
                                }
                            },
                            "status": {
                                "type": "D",
                                "description": "Delivered",
                                "code": "FS"
                            },
                            "date": "20200817",
                            "time": "145339"
                        },

I would like to save only a couple things from this JSON. for example [trackingNumber, city, country, type and description] but I don't understand which structure from java classes I should make to map them.我只想保存此 JSON 中的一些内容。例如[trackingNumber、城市、国家/地区、类型和描述] ,但我不明白我应该从 java 类到 map 中使用哪个结构。

Since you need few fields from the JSON I suggest you to use JSONPath API, below is the maven dependency for the same.由于您需要JSON中的几个字段,我建议您使用 JSONPath API,下面是相同的 maven 依赖项。

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

below is the code to get desired fields下面是获取所需字段的代码

public static void main(String[] args) throws IOException  {
    String json = "{\r\n" + 
            "   \"trackResponse\":{\r\n" + 
            "      \"shipment\":[\r\n" + 
            "         {\r\n" + 
            "            \"package\":[\r\n" + 
            "               {\r\n" + 
            "                  \"trackingNumber\":\"Y0081040217\",\r\n" + 
            "                  \"deliveryDate\":[\r\n" + 
            "                     {\r\n" + 
            "                        \"type\":\"DEL\",\r\n" + 
            "                        \"date\":\"20200817\"\r\n" + 
            "                     }\r\n" + 
            "                  ],\r\n" + 
            "                  \"deliveryTime\":{\r\n" + 
            "                     \"startTime\":\"\",\r\n" + 
            "                     \"endTime\":\"145339\",\r\n" + 
            "                     \"type\":\"DEL\"\r\n" + 
            "                  },\r\n" + 
            "                  \"activity\":[\r\n" + 
            "                     {\r\n" + 
            "                        \"location\":{\r\n" + 
            "                           \"address\":{\r\n" + 
            "                              \"city\":\"TORONTO\",\r\n" + 
            "                              \"stateProvince\":\"\",\r\n" + 
            "                              \"postalCode\":\"\",\r\n" + 
            "                              \"country\":\"CA\"\r\n" + 
            "                           }\r\n" + 
            "                        },\r\n" + 
            "                        \"status\":{\r\n" + 
            "                           \"type\":\"D\",\r\n" + 
            "                           \"description\":\"Delivered\",\r\n" + 
            "                           \"code\":\"FS\"\r\n" + 
            "                        },\r\n" + 
            "                        \"date\":\"20200817\",\r\n" + 
            "                        \"time\":\"145339\"\r\n" + 
            "                     }\r\n" + 
            "                  ]\r\n" + 
            "               }\r\n" + 
            "            ]\r\n" + 
            "         }\r\n" + 
            "      ]\r\n" + 
            "   }\r\n" + 
            "}";
    
    String[] jsonPaths = new String[5];
    jsonPaths[0] = "$.trackResponse.shipment[*].package[*].trackingNumber";
    jsonPaths[1] = "$.trackResponse.shipment[*].package[*].activity[*].location.address.city";
    jsonPaths[2] = "$.trackResponse.shipment[*].package[*].activity[*].location.address.country";
    jsonPaths[3] = "$.trackResponse.shipment[*].package[*].activity[*].status.description";
    jsonPaths[4]= "$.trackResponse.shipment[*].package[*].activity[*].status.type";
    
    DocumentContext jsonContext = JsonPath.parse(json);
    for (String path : jsonPaths) {
        List<String> result = jsonContext.read(path);
        System.out.println(result.get(0)); //assuming there is only one array else u can iterate
    }       
}

Result结果

Y0081040217
TORONTO
CA
Delivered
D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM