如何根据对象的另一个值获取特定的JSON对象?

时间:2012-06-28 04:16:35

标签: android json object

我是这样的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"获取"bulan" = 5的所有值。怎么做? 我想要的是这样的:

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

5 个答案:

答案 0 :(得分: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());

    }

}

答案 1 :(得分:1)

使用地图。

地图中的关键字是您想要合计的布兰值。

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

答案 2 :(得分:1)

我认为这一行有问题

布兰== 6

试试这种方式......

bulan.equals( “6”)

我认为它会对你有所帮助。

由于

答案 3 :(得分:1)

您的代码似乎是正确的。

哈希地图不允许使用dupilcate键。在这里你试图放置重复键。

请尝试使用ArrayList

Sonething是这样的:

ArrayList < String > listBulan4 = new ArrayList < String > ();
  listBulan4.add(object.getString("total"));

答案 4 :(得分: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)
}