0

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"
}
]
3
  • The documentation is your friend. It shows that DecimalFormat inherits two format methods from NumberFormat: format(double) and format(long). You are trying to format a String, but the methods only accept numbers. Commented Oct 7, 2020 at 13:13
  • @VGR Edited the question Commented Oct 7, 2020 at 14:16
  • Getting closer. Now, it should be obvious from the exception you’re getting that the characters USD do not constitute a valid float value. Use the substring method to obtain text consisting of only the numeric value. Commented Oct 7, 2020 at 14:47

1 Answer 1

1

It seems you mixed up the operation of JSONArray with JSONObject. The inner for-loop is supposed to traverse a JSONArray rather than a JSONObject.

And the format of promise_amount in your given JSON string is a String, so you can just append .00 to the origin value as follows:

Code snippet

for (int n = 0; n < result.length(); n++) {
    JSONArray object = result.getJSONObject(n).getJSONArray("promise_detail");
    for (int k = 0; k < object.length(); k++) {
        String promiseAmount = object.getJSONObject(k).getString("promise_amount");
        if (promiseAmount != null) {
            object.getJSONObject(k).put("promise_amount", String.format("%s.00", promiseAmount));
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Sachin HR Please let me know if my solution works for you, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.