Home > database >  How to wrap object in array in java?
How to wrap object in array in java?

Time:09-28

I'm making a code to send data to the payment gateway. I am a beginner in using java. I'm having a bit of trouble working with objects in an array in the item_details parameter. I've made the code as below.

public KeyValuePair<String, String> requestSnapToken(String orderNo, 
        BigDecimal grossAmount, int paymentMethodId,
        String firstName,
        String email, 
        List<FormCartLine> itemDetails 
        ) {
    return FAILSAFE_EXECUTOR.get(() -> {
        String midtransOrderId = orderNo;
        Builder<String, Object> custDetailBuilder = new ImmutableMap.Builder<String, Object>()
            .put("first_name", firstName)
            .put("last_name", "");
        Builder<String, Object> itemDetailsBuilder = new ImmutableMap.Builder<String, Object>();

        for (int i = 0; i < itemDetails.size(); i  ) { 
            FormCartLine line = itemDetails.get(i); 
            int row = i   1;  
            itemDetailsBuilder.put($("id%s", row), line.getProductId()) 
                .put($("price%s", row), line.getUnitPricePreTax()) 
                .put($("quantity%s", row), line.getQty()) 
                .put($("name%s", row), line.getName()) 
                .put($("brand%s", row), line.getBrandName()) 
                .put($("category%s", row), line.getCategoryName()); 
        } 
    
        Builder<String, Object> payloadBuilder = new ImmutableMap.Builder<String, Object>()
            .put("transaction_details", new ImmutableMap.Builder<String, Object>()
                    .put("order_id", midtransOrderId)
                    .put("gross_amount", String.valueOf(grossAmount.longValue()))
                    .build())
            .put("item_details", itemDetailsBuilder.build())
    });
}

I tried to debug from the code that I made, the payload looks like this. This is not what I wanted..

  "transaction_details" : {
    "order_id" : "SO-ON-000010__1664358135415",
    "gross_amount" : "194547"
  },
  "item_details" : {
    "id1" : "001",
    "price1" : 200,
    "quantity1" : 1,
    "name1" : "Dummy 1",
    "brand1" : "Dummy",
    "category1" : "Dummies",
    "id2" : "002",
    "price2" : 300,
    "quantity2" : 1,
    "name2" : "Dummy 2",
    "brand2" : "Dummy",
    "category2" : "Dummies"
  },

what i want is like below. How do I implement it in java?

  "transaction_details" : {
    "order_id" : "SO-ON-000010__1664358135415",
    "gross_amount" : "194547"
  },
  "item_details" : [{
    "id" : "001",
    "price" : 200,
    "quantity" : 1,
    "name" : "Dummy 1",
    "brand" : "Dummy",
    "category" : "Dummies"
  },
  {

    "id" : "002",
    "price" : 300,
    "quantity" : 1,
    "name" : "Dummy 2",
    "brand" : "Dummy",
    "category" : "Dummies"
  }
]

CodePudding user response:

Build separate maps for each of your objects and put those maps into a list. Immutability is probably not required, so I'll simplify by using an ArrayList:

final List<Map<String, Object>> itemDetailsList = new ArrayList<>(itemDetails.size());
for (final FormCartLine line : itemDetails) {
    itemDetailsList.add(Map.ofEntries(
            Map.entry("id", line.getProductId()),
            Map.entry("price", line.getUnitPricePreTax()),
            // etc.
    ));
}

// ...

Builder<String, Object> payloadBuilder = new ImmutableMap.Builder<String, Object>()
    // .put(...)
    .put("item_details", itemDetailsList)

Note that the map returned by Map.ofEntries cannot hold null values, so if that's a requirement, you need to switch to a HashMap<String, Object> or another map type with support for null values.

Of course, you can use an ImmutableMap.Builder too if you prefer or the loop could be converted to use the Java stream API.

  • Related