jeudi 20 septembre 2018

Get all values for a field from nested java object.

I've below JAVA object. I need to iterate through this nested object and fetch all values for one field. Ex 'time' for below java object. If it is a list, I can use java 8 filters. But how to do this on an object?

Also, I need to do it in a generic way.

    {
  "dataType": "Node",
  "totalCount": 1,
  "count": 1,
  "startIndex": 0,
  "data": [
    {
      "id": "a4b7a825f67930965747445709011120-Node-6f638b5e71debd5807ec7fb73b9dc20b",
      "refObjects": {},
      "tid": "a4b7a825f67930965747445709011120",
      "creationDate": "2018-09-20T06:55:36.742+0000",
      "lmd": "2018-09-20T06:55:36.799+0000",
      "exceptions": [
        {
          "name": "projectedInventory",
          "status": "Stockout",
          "severity": "High",
          "time": "2018-09-20T00:00:00.000+0000"
        }
      ],
      "criticalities": [
        "HotItem"
      ],
      "customerName": "Best Buys",
      "supplierName": "Samsung",
      "customerItemName": "Ship to item name",
      "nodeType": "inventory",
      "supplierItemName": "Ship from item name",
      "shipToSiteName": "IT06",
      "shipFromSiteName": "IT07",
      "status": "Active",
      "lob": "HC",
      "processType": "demandSupply",
      "measures": {
        "maxInventory": [
          {
            "refObjects": {},
            "time": "2018-09-26T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-09-27T00:00:00.000+0000",
            "quantity": 0
          }
        ],
        "maxDistribution": [
          {
            "refObjects": {},
            "time": "2018-09-28T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-09-29T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-09-30T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-10-07T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-10-14T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-10-21T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-10-28T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-11-04T00:00:00.000+0000",
            "quantity": 0
          },
          {
            "refObjects": {},
            "time": "2018-11-25T00:00:00.000+0000",
            "quantity": 0
          }
        ]
      },
      "customerItemDescription": "EXP08CN1W6  PORTABLE AIR CONDITIONER HC",
      "materialGroup": "ELX",
      "shipToSiteDescription": "IT - BE10 Porcia Hub"
    }
  ],
  "typeCounts": null
}

Now I want to retrieve all the values of "time" field and save it in a list. What's the best approach to do this? What can be the best way to do this?

the output should be something like this:

{
  "time": [
    "2018-12-30T00:00:00.000+0000",
    "2018-08-24T12:00:00.000+0000"
  ]
}





Aucun commentaire:

Enregistrer un commentaire