0

如何从 json 数组中过滤值我想过滤如果 string zero1check =0 在 Category_name.add(object.getString("dish_name")) 中仅添加该 json 数组;其中“天”:“m1”

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

  {
  "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]);

                }
4

1 回答 1

0

试试这个..然后将 zero1check 数据类型更改为 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"));
                            }

                        }

                    }
于 2013-08-27T07:47:39.147 回答