I have json array in this format
[
{
"promise_detail": [
{
"promise_date": "15 OCT 2020",
"promise_amount": "USD 1086",
"status": "CANCELLED"
}
],
"promise_note": "hello"
}
]
My requirement is i need to loop through Json array and format promise amount field to two decimal places which looks like "USD 1086.00"
Here is what i am trying to do
result = executeQueries.getPromiseHistoryDetails(promiseHistoryModel);//Here result is same Json Array as shown above
for (int n = 0; n < result.length(); n++) {
JSONObject object = result.getJSONObject(n);
for(int k=0;k<object.length();k++) { //struck here
String promiseAmount = object.getJSONObject(k);
Float amount=Float.parseFloat(promiseAmount);
if (object.get("promise_amount") != null) {
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
result.getJSONObject(n).put("promise_amount", df.format(amount));
}
}
}
But I am getting some issues.I am struck at this point.Can anyone suggest me how i can achieve this.This is my expected output
[
{
"promise_detail": [
{
"promise_date": "15 OCT 2020",
"promise_amount": "USD 1086.00",
"status": "CANCELLED"
}
],
"promise_note": "hello"
}
]
formatmethods from NumberFormat: format(double) and format(long). You are trying to format a String, but the methods only accept numbers.USDdo not constitute a valid float value. Use the substring method to obtain text consisting of only the numeric value.