2016-12-06 100 views
-2

我想將我解析的第一個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(); 
    } 
} 
+0

你要計算的金額之和? – rafid059

+0

是的,先生。那就是我想要123 + 123123 –

回答

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); 
相關問題