如何在每个循环中的变量中添加值?

时间:2016-12-06 07:42:12

标签: java json loops

我想将我解析的第一个Json字符串添加到int并添加然后将其添加到下一个Json字符串。

I/System.out: [{"checkdate":"Dec 5, 2016","checkno":"1231212","datepaid":"Dec 5, 2016","clientname":"sjkdfh","bank":"asj","amount":"123","status":"UNFINISHED"},{"checkdate":"Dec 5, 2016","checkno":"123","datepaid":"Dec 5, 2016","clientname":"asdjaksd","bank":"sandakjsn","amount":"123123","status":"UNFINISHED"}]
I/System.out: 123
I/System.out: 123123

我想将123添加到123123这里是我的代码

JSONObject jsonResponse = new JSONObject(response);
JSONArray arr = jsonResponse.getJSONArray("message");
boolean success = jsonResponse.getBoolean("success");

if (success) {
    System.out.println(arr);
    if (arr != null) {
        for(int i = 0;i<arr.length();i++){
            listdata.add(arr.get(i).toString());
            String money = arr.getJSONObject(i).getString("amount");
            int money1 = Integer.parseInt(money);

            System.out.println(money1);
        }

        arrayAdapter.notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:1)

int sum = 0;
for(int i = 0;i<arr.length();i++){
    listdata.add(arr.get(i).toString());
    String money = arr.getJSONObject(i).getString("amount");
    sum += Integer.parseInt(money);
}
System.out.println(sum);