2

我有这样的json数组:

{"GetReportResult":
  [
   {"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"}, 
   {"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},
   {"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},
   {"bulan":"5","total":"5703","type":"CHEESE1K","uang":"134929306"},
   {"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},
   {"bulan":"5","total":"51958","type":"WHP","uang":"631994613"},
   {"bulan":"6","total":"6190","type":"CHEESE1K","uang":"104527773"},
   {"bulan":"6","total":"443335","type":"ESL","uang":"4540123082"},
   {"bulan":"6","total":"30550","type":"ESL500ML","uang":"148302378"},
  ]
}

这是我想要获取某个对象的代码:

JSONArray  weeklyflash = json.getJSONArray("GetReportResult");
JSONObject e = weeklyflash.getJSONObject(i);     
String bulan = e.getString("bulan");

if (bulan == 6){
     map.put("total1", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 6)
}
else if (bulan == 5){
     map.put("total2", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 5)
}
else if (bulan == 4){
      map.put("total3", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 4)
}

我得到的是这样的:

  total1  |   total2   |  total3
----------------------------------
  2448    |   2448     |  2448
  572476  |   572476   |  572476
  46008   |   46008    |  46008

我想获取来自指定对象值的值(例如,我想从"total"where "bulan"= 5 获取所有值)。怎么做?我想要的是这样的:

  total1  |   total2   |  total3
----------------------------------
  6190    |   5703     |  2448
  443335  |   648663   |  572476
  30550   |   51958    |  46008
4

5 回答 5

2

干得好

try {
    ArrayList < String > listBulan4 = new ArrayList < String > ();
    ArrayList < String > listBulan5 = new ArrayList < String > ();
    ArrayList < String > listBulan6 = new ArrayList < String > ();

    JSONObject jsonObject = new JSONObject("jsonString");

    JSONArray array = jsonObject.getJSONArray("GetReportResult");

    for (int i = 0; i < array.length(); i++) {
        JSONObject object = array.getJSONObject(i);

        int bulanValue = Integer.parseInt(object.getString("bulan"));

        switch (bulanValue) {
        case 4:
            listBulan4.add(object.getString("total"));
            break;
        case 5:
            listBulan5.add(object.getString("total"));
            break;
        case 6:
            listBulan6.add(object.getString("total"));
            break;
        }
    }

    Map < String, ArrayList < String >> map = new HashMap < String, ArrayList < String >> ();
    map.put("total1", listBulan6);
    map.put("total2", listBulan5);
    map.put("total3", listBulan4);

} catch (Exception e) {}

下面是代码。

package org.sample;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SOActivity extends Activity {

    String string = "{\"GetReportResult\":[{\"bulan\":\"4\",\"total\":\"2448\",\"type\":\"CHEESE1K\",\"uang\":\"8847823\"},{\"bulan\":\"4\",\"total\":\"572476\",\"type\":\"ESL\",\"uang\":\"5863408410\"},{\"bulan\":\"4\",\"total\":\"46008\",\"type\":\"ESL500ML\",\"uang\":\"234498301\"},{\"bulan\":\"5\",\"total\":\"5703\",\"type\":\"CHEESE1K\",\"uang\":\"134929306\"},{\"bulan\":\"5\",\"total\":\"648663\",\"type\":\"ESL\",\"uang\":\"6645764498\"},{\"bulan\":\"5\",\"total\":\"51958\",\"type\":\"WHP\",\"uang\":\"631994613\"},{\"bulan\":\"6\",\"total\":\"6190\",\"type\":\"CHEESE1K\",\"uang\":\"104527773\"},{\"bulan\":\"6\",\"total\":\"443335\",\"type\":\"ESL\",\"uang\":\"4540123082\"},{\"bulan\":\"6\",\"total\":\"30550\",\"type\":\"ESL500ML\",\"uang\":\"148302378\"}]}";
    Map < String, ArrayList < String >> map;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        map = new HashMap < String, ArrayList < String >> ();
        try {
            ArrayList < String > listBulan4 = new ArrayList < String > ();
            ArrayList < String > listBulan5 = new ArrayList < String > ();
            ArrayList < String > listBulan6 = new ArrayList < String > ();

            JSONObject jsonObject = new JSONObject(string);

            JSONArray array = jsonObject.getJSONArray("GetReportResult");

            for (int i = 0; i < array.length(); i++) {
                JSONObject object = array.getJSONObject(i);

                int bulanValue = Integer.parseInt(object.getString("bulan"));

                switch (bulanValue) {
                case 4:
                    listBulan4.add(object.getString("total"));
                    break;
                case 5:
                    listBulan5.add(object.getString("total"));
                    break;
                case 6:
                    listBulan6.add(object.getString("total"));
                    break;
                }
            }

            map.put("total1", listBulan6);
            map.put("total2", listBulan5);
            map.put("total3", listBulan4);

        } catch (Exception e) {
            e.printStackTrace();
        }

        Log.i("test", map.toString());

    }

}
于 2012-06-28T05:13:32.187 回答
1

使用地图。

Map 中的键是您想要总计的 Bulan 值。

迭代数组并更新相关bulan值的总和。

于 2012-06-28T04:20:41.257 回答
1

我认为这条线有问题

不蓝==6

试试这个方法...

bulan.equals("6")

我认为它对你有帮助。

谢谢

于 2012-06-28T04:21:53.643 回答
1

您的代码似乎正确。

哈希映射不允许重复键。在这里您尝试放置重复键。

所以尝试ArrayList

像这样的事情:

ArrayList < String > listBulan4 = new ArrayList < String > ();
  listBulan4.add(object.getString("total"));
于 2012-06-28T08:24:28.917 回答
0

你的代码对我来说看起来不错。你试一试

  if (bulan.equals("6")){
  map.put("total1", e.getString("total")); // I want the "total" values are from       specified bulan (e.g. bulan == 6)
}
else if (bulan.equals("5")){
     map.put("total2", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 5)
 }
  else if (bulan.equals("4")){
     map.put("total3", e.getString("total")); // I want the "total" values are from specified bulan (e.g. bulan == 4)
}
于 2012-06-28T05:00:22.877 回答