如何仅从json文件中获取某些属性?

时间:2019-02-06 12:14:51

标签: java json jackson

我有一个看起来像这样的json文件:

从此json,我只需要获取每个航班的总价。而且我需要以一种方式(即段列表大小)获取航班数量。我已经使用http://www.jsonschema2pojo.org/创建了很多对象。通过大量的for循环,我获得了必要的数据。但是我的问题是:有没有更简单的方法可以从此json文件中获取此信息?

{
  "type": "flight-offer",
  "id": "1549454712606-1539032420",
  "offerItems": [
    {
      "services": [
        {
          "segments": [
            {
              "flightSegment": {
                "departure": {
                  "iataCode": "SJJ",
                  "at": "2019-02-08T06:30:00+01:00"
                },
                "arrival": {
                  "iataCode": "ZAG",
                  "at": "2019-02-08T07:20:00+01:00"
                },
                "carrierCode": "OU",
                "number": "341",
                "aircraft": {
                  "code": "DH4"
                },
                "operating": {
                  "carrierCode": "OU",
                  "number": "341"
                },
                "duration": "0DT0H50M"
              },
              "pricingDetailPerAdult": {
                "travelClass": "ECONOMY",
                "fareClass": "K",
                "availability": 3,
                "fareBasis": "KEASY2"
              }
            }
          ]
        },
        {
          "segments": [
            {
              "flightSegment": {
                "departure": {
                  "iataCode": "ZAG",
                  "at": "2019-02-15T14:10:00+01:00"
                },
                "arrival": {
                  "iataCode": "SJJ",
                  "at": "2019-02-15T15:00:00+01:00"
                },
                "carrierCode": "OU",
                "number": "344",
                "aircraft": {
                  "code": "DH4"
                },
                "operating": {
                  "carrierCode": "OU",
                  "number": "344"
                },
                "duration": "0DT0H50M"
              },
              "pricingDetailPerAdult": {
                "travelClass": "ECONOMY",
                "fareClass": "U",
                "availability": 9,
                "fareBasis": "UEASY2"
              }
            }
          ]
        }
      ],
      "price": {
        "total": "164.09",
        "totalTaxes": "83.09"
      },
      "pricePerAdult": {
        "total": "164.09",
        "totalTaxes": "83.09"
      }
    }
  ]
},
{
  "type": "flight-offer",
  "id": "1549454712606--1458421402",
  "offerItems": [
    {
      "services": [
        {
          "segments": [
            {
              "flightSegment": {
                "departure": {
                  "iataCode": "SJJ",
                  "at": "2019-02-08T15:10:00+01:00"
                },
                "arrival": {
                  "iataCode": "BEG",
                  "terminal": "2",
                  "at": "2019-02-08T16:00:00+01:00"
                },
                "carrierCode": "JU",
                "number": "113",
                "aircraft": {
                  "code": "AT7"
                },
                "operating": {
                  "carrierCode": "JU",
                  "number": "113"
                },
                "duration": "0DT0H50M"
              },
              "pricingDetailPerAdult": {
                "travelClass": "ECONOMY",
                "fareClass": "U",
                "availability": 7,
                "fareBasis": "UNBRBA"
              }
            },
            {
              "flightSegment": {
                "departure": {
                  "iataCode": "BEG",
                  "terminal": "2",
                  "at": "2019-02-08T18:20:00+01:00"
                },
                "arrival": {
                  "iataCode": "ZAG",
                  "at": "2019-02-08T19:35:00+01:00"
                },
                "carrierCode": "JU",
                "number": "234",
                "aircraft": {
                  "code": "AT7"
                },
                "operating": {
                  "carrierCode": "JU",
                  "number": "234"
                },
                "duration": "0DT1H15M"
              },
              "pricingDetailPerAdult": {
                "travelClass": "ECONOMY",
                "fareClass": "U",
                "availability": 7,
                "fareBasis": "UNBRBA"
              }
            }
          ]
        },
        {
          "segments": [
            {
              "flightSegment": {
                "departure": {
                  "iataCode": "ZAG",
                  "at": "2019-02-15T10:00:00+01:00"
                },
                "arrival": {
                  "iataCode": "BEG",
                  "terminal": "2",
                  "at": "2019-02-15T11:05:00+01:00"
                },
                "carrierCode": "JU",
                "number": "231",
                "aircraft": {
                  "code": "AT7"
                },
                "operating": {
                  "carrierCode": "JU",
                  "number": "231"
                },
                "duration": "0DT1H5M"
              },
              "pricingDetailPerAdult": {
                "travelClass": "ECONOMY",
                "fareClass": "U",
                "availability": 7,
                "fareBasis": "UNBRBA"
              }
            },
            {
              "flightSegment": {
                "departure": {
                  "iataCode": "BEG",
                  "terminal": "2",
                  "at": "2019-02-15T13:50:00+01:00"
                },
                "arrival": {
                  "iataCode": "SJJ",
                  "at": "2019-02-15T14:40:00+01:00"
                },
                "carrierCode": "JU",
                "number": "112",
                "aircraft": {
                  "code": "AT7"
                },
                "operating": {
                  "carrierCode": "JU",
                  "number": "112"
                },
                "duration": "0DT0H50M"
              },
              "pricingDetailPerAdult": {
                "travelClass": "ECONOMY",
                "fareClass": "U",
                "availability": 7,
                "fareBasis": "UNBRBA"
              }
            }
          ]
        }
      ],
      "price": {
        "total": "225.45",
        "totalTaxes": "145.45"
      },
      "pricePerAdult": {
        "total": "225.45",
        "totalTaxes": "145.45"
      }
    }
  ]
}

2 个答案:

答案 0 :(得分:2)

您的JSON包含两个用逗号分隔的单独对象。您需要添加方括号:

[{...},{...}]

如果只需要price,则可以阅读以下内容的JSON

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(source);

List<JsonNode> prices = node.findValues("price");
for (JsonNode price : prices) {
    System.out.println(price.get("total"));
    System.out.println(price.get("totalTaxes"));
}

编辑
您可以通过类似的方式找到flighSegment

ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(jsonFile);

List<JsonNode> segments = node.findValues("segments");
for (JsonNode segment : segments) {
    List<JsonNode> flightSegments = segment.findValues("flightSegment");
    System.out.println(flightSegments.size());
}

有关更多信息,请参见:

  1. Working with Tree Model Nodes in Jackson

答案 1 :(得分:0)

适合此工作的工具是JsonPath,它允许对JSON树进行类似XPath的查询。 这是获取请求信息的示例

String json = new String(Files.readAllBytes(Paths.get("c:/temp/xx.json")));

// search for price object anywhere in the json doc and get total object under it
// the search returns a list because of the deep search operator ".." 
List<String> ls = JsonPath.read(json, "$..price.total");
String priceValueStr = ls.get(0);
System.out.println(priceValueStr);

// search for segments array anywhere in the json doc and return array's length
List<Integer> li = JsonPath.read(json, "$..segments.length()");
Integer segments = li.get(0);
System.out.println(segments);