英文:
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.
<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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论