简体   繁体   中英

Group a list of json object by property inside the common Array in Java

I would like to ask if it is possible to group the object by another object inside its common array.

Here's the JSON response, I need to group the list of item by program id . I'm trying to put it on the HashMap but it didn't work well.

{
"id": "",
"ordered_by": 64,
"order_details": [
    {
        "resource": "Product",
        "required_prescription": false,
        "item": {
            "id": 6,
            "name": "Synergistic Copper Gloves",
            "code": "51537661-C",
            "enabled": true,
            "generic_name": "Mediocre Steel Wallet",
            "price_cents": 200000
        },
        "program": {
            "id": 12,
            "name": "Synergistic Wooden Shoes",
            "provider": "Synergistic Rubber Coat",
            "discount_type": "fixed"
        }
    },
    {
        "resource": "Product",
        "required_prescription": true,
        "item": {
            "id": 7,
            "name": "Rustic Leather Table",
            "code": "74283131-P",
            "enabled": true,
            "generic_name": "Incredible Bronze Clock",
            "price_cents": 8994
        },
        "program": {
            "id": 12,
            "name": "Synergistic Wooden Shoes",
            "provider": "Synergistic Rubber Coat",
            "discount_type": "fixed"
        }
    },
    {
        "resource": "Product",
        "required_prescription": false,
        "item": {
            "id": 116,
            "name": "Ergonomic Marble Hat",
            "code": "98845056-A",
            "enabled": true,
            "generic_name": "Incredible Granite Lamp",
            "price_cents": 8267
        },
        "program": {
            "id": 10,
            "name": "Durable Rubber Bag",
            "provider": "Aerodynamic Steel Chair",
            "discount_type": "fixed"
        }
    }
]}

This should be the expected object after grouping. The item was grouped by program id 12 & 10.

[
  {
    "id": 12,
    "name": "Synergistic Wooden Shoes",
    "provider": "Synergistic Rubber Coat",
    "discount_type": "fixed",
    "item": [
      {
        "id": 6,
        "name": "Synergistic Copper Gloves",
        "code": "51537661-C",
        "enabled": true,
        "generic_name": "Mediocre Steel Wallet",
        "price_cents": 200000
      },
      {
        "id": 7,
        "name": "Rustic Leather Table",
        "code": "74283131-P",
        "enabled": true,
        "generic_name": "Incredible Bronze Clock",
        "price_cents": 8994
      }
    ]
  },
  {
    "id": 10,
    "name": "Durable Rubber Bag",
    "provider": "Aerodynamic Steel Chair",
    "discount_type": "fixed",
    "item": [
      {
        "id": 116,
        "name": "Ergonomic Marble Hat",
        "code": "98845056-A",
        "enabled": true,
        "generic_name": "Incredible Granite Lamp",
        "price_cents": 8267
      }
    ]
  }
]

All comments would be highly appreciated. Thanks in advance!

I have taken your source json and tried to convert it as per your specification and this is the solution which is working, pass your source JSON as string and you will get the desired output

private String parseJson(String source) {
    JSONArray result = new JSONArray();
    List<Integer> ids = new ArrayList<>();
    HashMap<Integer,JSONObject> programs = new HashMap<>();
    try {
        JSONObject jSource = new JSONObject(source);
        JSONArray orderDetails = jSource.getJSONArray("order_details");
        if (orderDetails.length() > 0) {
            for (int i = 0; i < orderDetails.length(); i++) {
                JSONObject jsonObject = orderDetails.getJSONObject(i);
                JSONObject item = jsonObject.getJSONObject("item");
                JSONObject program = jsonObject.getJSONObject("program");
                int programId = jsonObject.getJSONObject("program").getInt("id");
                if (!ids.contains(programId)) {
                    ids.add(programId);
                    program.put("item",new JSONArray().put(item));
                    programs.put(programId,program);
                }else{
                    program.put("item",programs.get(programId).getJSONArray("item").put(item));
                }
            }

            for(int k :programs.keySet()){
                result.put(programs.get(k));
            }

        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return result.toString();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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