Search code examples
javanested-loops

Loop rows using POJO to create JSON with missing items


I have the following rows from database :

ID    name   address1        address 2
-----------------------------------
123   Edvin  Hong Kong       Hong Kong
123   Edvin  Taipei          Taiwan
124   Advin  Bangkok         Thailand
-----------------------------------

I want to have the following JSON result:

"Item":[  
  {  "name": "Edvin"
     "addresses": [
        {
          "address1": "Hong Kong"
          "address2": "Hong Kong"
         } ,
        {
          "address1": "Taipei"
          "address2": "Taiwan"
        }
      ]
   },
   {  "name": "Advin"
     "addresses": [
        {
          "address1": "Bangkok"
          "address2": "Thaland"
         }
      ]
   }
]

I tried to do this:

List<Item> items= new ArrayList<Item>();
List<String> ids = new ArrayList<String>();

        for (Record record: records) { // Loop the rows show above
            if(!ids .contains(record.getId())) { //prevent duplicate Item
              Item item = new Item();
              item.setName(record.getSurname());
              Address address = new Address();
              address.setAddress1 = record.getAddress1();
              address.setAddress2 = record.getAddress2();
              item.setAddreses(address);
              items.add(item); // add the item into items
           }
           Ids.add(record.getId());
        }

The above code I can only get the first address of item name Edvin, how can I get the 2nd address for Edvin?


Solution

  • First of all, the object Item you have created is not suitable for the JSON you want to obtain. As far as I can see the object Item has a property of type Addresses which is ok to keep track of address1 and address2, but if you want the above JSON result you need a list of addresses as a property instead.

    Then, if you want to keep List items and List ids and assuming that you are collecting all the items with the same name the code should change this way:

    List<Item> items= new ArrayList<Item>();
    List<String> ids = new ArrayList<String>();
    
        for (Record record: records) { // Loop the rows show above
            if(!ids .contains(record.getId())) { //prevent duplicate Item
              Item item = new Item();
              item.setName(record.getSurname());
              Address address = new Address();
              address.setAddress1 = record.getAddress1();
              address.setAddress2 = record.getAddress2();
              List<Address> addrList = new ArrayList<Address>();
              addrList.add(address);
              item.setAddreses(addrList);
              items.add(item); // add the item into items
           } else {
             for (Item it: items) {
                if(it.getName().equals(record.getSurname()) {
                     Address addr = new Address();
                     addr.setAddress1 = record.getAddress1();
                     addr.setAddress2 = record.getAddress2();
                     it.getAddresses().add(addr);
                }
             }
           }
           Ids.add(record.getId());
        }
    


    Not the best solution really, in cases such this instead of using that List data structures I suggest to go on a Map that avoids duplicates by definition.