如何仅解析 JSON 的一部分?

huangapple go评论73阅读模式
英文:

How to Parse only part of 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"
              },

我想从这个 JSON 中仅保存一些内容。例如 [trackingNumber, city, country, type 和 description],但我不太明白我应该创建哪种结构的 Java 类来映射它们。

英文:

I get a 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.

答案1

得分: 1

由于您需要从 JSON 中获取少数字段,我建议您使用 JSONPath API,以下是相应的 Maven 依赖项。

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

以下是获取所需字段的代码。

public static void main(String[] args) throws IOException  {
    String json = "YOUR_JSON_CONTENT_HERE";
    
    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)); // 假设只有一个数组,否则您可以进行迭代
    }		
}

结果

Y0081040217
TORONTO
CA
Delivered
D
英文:

Since you need few fields from the JSON I suggest you to use JSONPath API , below is the maven dependency for the same.

&lt;dependency&gt;
    &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
    &lt;artifactId&gt;json-path&lt;/artifactId&gt;
    &lt;version&gt;2.4.0&lt;/version&gt;
&lt;/dependency&gt;

below is the code to get desired fields

public static void main(String[] args) throws IOException  {
	String json = &quot;{\r\n&quot; + 
			&quot;   \&quot;trackResponse\&quot;:{\r\n&quot; + 
			&quot;      \&quot;shipment\&quot;:[\r\n&quot; + 
			&quot;         {\r\n&quot; + 
			&quot;            \&quot;package\&quot;:[\r\n&quot; + 
			&quot;               {\r\n&quot; + 
			&quot;                  \&quot;trackingNumber\&quot;:\&quot;Y0081040217\&quot;,\r\n&quot; + 
			&quot;                  \&quot;deliveryDate\&quot;:[\r\n&quot; + 
			&quot;                     {\r\n&quot; + 
			&quot;                        \&quot;type\&quot;:\&quot;DEL\&quot;,\r\n&quot; + 
			&quot;                        \&quot;date\&quot;:\&quot;20200817\&quot;\r\n&quot; + 
			&quot;                     }\r\n&quot; + 
			&quot;                  ],\r\n&quot; + 
			&quot;                  \&quot;deliveryTime\&quot;:{\r\n&quot; + 
			&quot;                     \&quot;startTime\&quot;:\&quot;\&quot;,\r\n&quot; + 
			&quot;                     \&quot;endTime\&quot;:\&quot;145339\&quot;,\r\n&quot; + 
			&quot;                     \&quot;type\&quot;:\&quot;DEL\&quot;\r\n&quot; + 
			&quot;                  },\r\n&quot; + 
			&quot;                  \&quot;activity\&quot;:[\r\n&quot; + 
			&quot;                     {\r\n&quot; + 
			&quot;                        \&quot;location\&quot;:{\r\n&quot; + 
			&quot;                           \&quot;address\&quot;:{\r\n&quot; + 
			&quot;                              \&quot;city\&quot;:\&quot;TORONTO\&quot;,\r\n&quot; + 
			&quot;                              \&quot;stateProvince\&quot;:\&quot;\&quot;,\r\n&quot; + 
			&quot;                              \&quot;postalCode\&quot;:\&quot;\&quot;,\r\n&quot; + 
			&quot;                              \&quot;country\&quot;:\&quot;CA\&quot;\r\n&quot; + 
			&quot;                           }\r\n&quot; + 
			&quot;                        },\r\n&quot; + 
			&quot;                        \&quot;status\&quot;:{\r\n&quot; + 
			&quot;                           \&quot;type\&quot;:\&quot;D\&quot;,\r\n&quot; + 
			&quot;                           \&quot;description\&quot;:\&quot;Delivered\&quot;,\r\n&quot; + 
			&quot;                           \&quot;code\&quot;:\&quot;FS\&quot;\r\n&quot; + 
			&quot;                        },\r\n&quot; + 
			&quot;                        \&quot;date\&quot;:\&quot;20200817\&quot;,\r\n&quot; + 
			&quot;                        \&quot;time\&quot;:\&quot;145339\&quot;\r\n&quot; + 
			&quot;                     }\r\n&quot; + 
			&quot;                  ]\r\n&quot; + 
			&quot;               }\r\n&quot; + 
			&quot;            ]\r\n&quot; + 
			&quot;         }\r\n&quot; + 
			&quot;      ]\r\n&quot; + 
			&quot;   }\r\n&quot; + 
			&quot;}&quot;;
	
	String[] jsonPaths = new String[5];
	jsonPaths[0] = &quot;$.trackResponse.shipment[*].package[*].trackingNumber&quot;;
	jsonPaths[1] = &quot;$.trackResponse.shipment[*].package[*].activity[*].location.address.city&quot;;
	jsonPaths[2] = &quot;$.trackResponse.shipment[*].package[*].activity[*].location.address.country&quot;;
	jsonPaths[3] = &quot;$.trackResponse.shipment[*].package[*].activity[*].status.description&quot;;
	jsonPaths[4]= &quot;$.trackResponse.shipment[*].package[*].activity[*].status.type&quot;;
	
	DocumentContext jsonContext = JsonPath.parse(json);
	for (String path : jsonPaths) {
		List&lt;String&gt; 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

huangapple
  • 本文由 发表于 2020年9月2日 14:33:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63699945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定