[{“id”:“4”,“jenistrx”:“masuk”,“keterangantrx”:“masssuakn”,“jumlahtrx”:“50000”,“tgltrx”:“2017-11-25 23:45 :06“},{”id“:”5“,”jenistrx“:”keluar“,”keterangantrx“:”keluar“,”jumlahtrx“:”20000“,”tgltrx“:”2017-11-25 23: 45:27“},{”id“:”6“,”jenistrx“:”keluar“,”keterangantrx“:”biaya“,”jumlahtrx“:”5000“,”tgltrx“:”2017-11-26 08 :22:38" }]
我想计算从php获得的总“jumlahtrx”总和的总和,我正在使用JsonArray。
在班上
int Saldo = 0;
然后
@Override
protected Void doInBackground(Void... arg0)
{
// Passing HTTP URL to HttpServicesClass Class.
HttpServicesClass httpServicesClass = new HttpServicesClass(HttpUrl);
try
{
httpServicesClass.ExecutePostRequest();
if(httpServicesClass.getResponseCode() == 200)
{
JSonResult = httpServicesClass.getResponse();
if(JSonResult != null)
{
JSONArray jsonArray = null;
try {
jsonArray = new JSONArray(JSonResult);
int sumTotaljumlah = 0;
JSONObject jsonObject;
Student student;
studentList = new ArrayList<Student>();
for(int i=0; i<jsonArray.length(); i++)
{
student = new Student();
jsonObject = jsonArray.getJSONObject(i);
//String jenis = jsonObject.getString("jenistrx");
String total = jsonObject.getString("jumlahtrx");
sumTotaljumlah = Integer.parseInt(total);
Saldo = Saldo + sumTotaljumlah;
// Adding Student Id TO IdList Array.
IdList.add(jsonObject.getString("id").toString());
//Adding Student Name.
student.JenisTrx = jsonObject.getString("jenistrx").toString();
student.JumlahTrx = jsonObject.getString("jumlahtrx").toString();
studentList.add(student);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else
{
Toast.makeText(context, httpServicesClass.getErrorMessage(), Toast.LENGTH_SHORT).show();
}
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
以下是如何展示
@Override
protected void onPostExecute(Void result)
{
progressBar.setVisibility(View.GONE);
StudentListView.setVisibility(View.VISIBLE);
TextView textViewTotal = (TextView)findViewById(R.id.textViewTotal);
textViewTotal.setText("Saldo: Rp. "+Saldo);
ListAdapterClass adapter = new ListAdapterClass(studentList, context);
StudentListView.setAdapter(adapter);
}
对此有何解决方法?
答案 0 :(得分:-1)
首先,你必须使用
json_decode($yourresponse)
检索数组。
然后,你用来获取
$1 = $yourresponse[0]["jumlahtrx"]
$2 = $yourresponse[1]["jumlahtrx"];
也许
bcadd($1,$2);
等。