英文:
Iterating through nested Json Array and performing operations on String Java
问题
[
{
"promise_detail": [
{
"promise_date": "15 OCT 2020",
"promise_amount": "USD 1086.00",
"status": "CANCELLED"
}
],
"promise_note": "hello"
}
]
result = executeQueries.getPromiseHistoryDetails(promiseHistoryModel);
for (int n = 0; n < result.length(); n++) {
JSONObject object = result.getJSONObject(n);
JSONArray promiseDetailArray = object.getJSONArray("promise_detail"); // Access promise_detail array
for (int k = 0; k < promiseDetailArray.length(); k++) {
JSONObject promiseDetail = promiseDetailArray.getJSONObject(k);
String promiseAmountStr = promiseDetail.getString("promise_amount"); // Access promise_amount string
String formattedPromiseAmount = promiseAmountStr.replace("USD", "").trim(); // Remove "USD" and leading/trailing spaces
Float amount = Float.parseFloat(formattedPromiseAmount);
DecimalFormat df = new DecimalFormat("0.00");
df.setMaximumFractionDigits(2);
promiseDetail.put("promise_amount", "USD " + df.format(amount)); // Update promise_amount field
}
}
英文:
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"
}
]
答案1
得分: 1
似乎您在操作中混淆了 JSONArray
和 JSONObject
。内部的循环应遍历 JSONArray
而不是 JSONObject
。
而且您给定的 JSON 字符串中 promise_amount
的格式是一个 String
,所以您可以将 .00
直接附加到原始值,如下所示:
代码片段
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));
}
}
}
英文:
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));
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论