从JSON总结数据

时间:2014-07-08 01:47:39

标签: java android json sum

我有一些JSON数据可以求和,但是我收到错误。这是我的代码:

protected String doInBackground(Object... params)
{
    JSONParser jParser = new JSONParser();
    JSONObject json = jParser.AmbilJson(link_url);
    int Total = 0;
    try
    {
        pesanan = json.getJSONArray("pesanan");
        for (int i = 0; i < pesanan.length(); i++)
        {
            JSONObject ar = pesanan.getJSONObject(i);
            Total = ar.getInt("Total");
            Jumlah = Jumlah + Total;
        }
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(String s)
{
    super.onPostExecute(s);
    Totalbayar.setText(Jumlah);

**我的Php **

     <?php
      $kd = $_GET['meja'];
      include("koneksi.php");
      $q = mysql_query('SELECT * FROM tbl_pesanan where Id_meja ="'.$kd.'"');
      $v = '{"pesanan" : [';
      while($r=mysql_fetch_array($q))
     {
        $ob = array();
        $v .= '{"Total" : "'.str_replace($ob,' ',strip_tags($r["Total"])).'",}';


      $v .= ']}';
       echo $v;
       ?>
        }

JSON数据:

{"pesanan" : [{"Total" : "20000"},{"Total" : "10000"},{"Total" : "27000"}]}

logcat的:

07-08 08:25:50.487:E / AndroidRuntime(621):致命异常:主要     07-08 08:25:50.487:E / AndroidRuntime(621):android.content.res.Resources $ NotFoundException:字符串资源ID#0x0     07-08 08:25:50.487:E / AndroidRuntime(621):在android.content.res.Resources.getText(Resources.java:247)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.widget.TextView.setText(TextView.java:3428)     07-08 08:25:50.487:E / AndroidRuntime(621):at aplikasi.slluberscafe.TungguPesanan $ tampil.onPostExecute(TungguPesanan.java:95)     07-08 08:25:50.487:E / AndroidRuntime(621):at aplikasi.slluberscafe.TungguPesanan $ tampil.onPostExecute(TungguPesanan.java:1)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.os.AsyncTask.finish(AsyncTask.java:602)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.os.AsyncTask.access $ 600(AsyncTask.java:156)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.os.AsyncTask $ InternalHandler.handleMessage(AsyncTask.java:615)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.os.Handler.dispatchMessage(Handler.java:99)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.os.Looper.loop(Looper.java:137)     07-08 08:25:50.487:E / AndroidRuntime(621):在android.app.ActivityThread.main(ActivityThread.java:4340)     07-08 08:25:50.487:E / AndroidRuntime(621):at java.lang.reflect.Method.invokeNative(Native Method)     07-08 08:25:50.487:E / AndroidRuntime(621):at java.lang.reflect.Method.invoke(Method.java:511)     07-08 08:25:50.487:E / AndroidRuntime(621):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)     07-08 08:25:50.487:E / AndroidRuntime(621):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)     07-08 08:25:50.487:E / AndroidRuntime(621):at dalvik.system.NativeStart.main(Native Method)

我想总结变量Total

1 个答案:

答案 0 :(得分:0)

你的json数组[{"Total" : "20000"}{"Total" : "10000"}{"Total" : "27000"}]必须用逗号分隔,

<强>更新

<?php $kd = $_GET['meja']; include("koneksi.php"); $q = mysql_query('SELECT * FROM tbl_pesanan where Id_meja ="'.$kd.'"');
//My update $output['pesanan'] = array(); while($r=mysql_fetch_array($q)) { array_push($output['pesanan'], array('Total' => $r['Total'])); } echo json_encode($output); ?>

更新2 此资源错误更新。您正在为Integer设置Text值。因此,编译器将查找string.xml中与您的Jumlah值匹配的文本。所以你必须将Jumlah转换为String: Totalbayar.setText(String.valueOf(Jumlah));