如何通过某种条件过滤json数组

时间:2013-08-27 07:19:32

标签: android

如何过滤json数组中的值我想过滤,如果字符串zero1check = 0只添加Category_name.add中的json数组(object.getString(“dish_name”));其中“天”:“m1”

如果zero1check = 0则添加Category_name.add(object.getString(“dish_name”));只有那个包含“day”的json数组:“m2”

  {
  "status":1,
  "data":
    [
    {
   "school_name":"testing12",
     "menu_title":"Menu1",
    "dish_name":null,
   "day":"m1"
    }
   ,
    {
     "school_name":"testing12"
   ,"menu_title":"Menu1",
      "dish_name":null,
      "day":"m1"
     }

     ,
    {
     "school_name":"testing12"
     ,"menu_title":"Menu1",
  "dish_name":null,
      "day":"m2"
   }


   ,
    {
    "school_name":"testing12"
   ,"menu_title":"Menu1",
   "dish_name":null,
   "day":"m2"
     }
    ]
    }



     static ArrayList<Long> Category_ID = new ArrayList<Long>();
 static ArrayList<String> Category_name = new ArrayList<String>();
 static ArrayList<String> menu_name = new ArrayList<String>();

         String  zero1check;



            JSONObject json2 = new JSONObject(str);

            status = json2.getString("status");
            if (status.equals("1")) {



            JSONArray school = json2.getJSONArray("data");

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


                Category_ID.add((long) i);
                Category_name.add(object.getString("dish_name"));




                menu_name.add(object.getString("menu_title"));



                String[] mVal = new String[school.length()];

                for (int k = 0; k < school.length(); k++) {
                    mVal[k] = 
                 school.getJSONObject(k).getString("menu_title");

                    menu_nametxt.setText(mVal[0]);

                }

1 个答案:

答案 0 :(得分:0)

试试这个..然后将zero1检查数据类型更改为int

 JSONArray school = json2.getJSONArray("data");

                    for (int i1 = 0; i1 < school.length(); i1++) {
                        JSONObject object = school.getJSONObject(i1);

                        if(zero1check == 0)
                        {

                            if(object.getString("id").equals("m1"))
                            {
                                Category_name.add(object.getString("dish_name"));
                            }

                        }
                        else if(zero1check == 1)
                        {

                            if(object.getString("id").equals("m2"))
                            {
                                Category_name.add(object.getString("dish_name"));
                            }

                        }

                    }