0

下面是我的代码,我只想过滤名称为小学的学校,但我的代码不过滤它,我删除了这一行if (object.getString("title") =="Elementary Schools"),然后显示所有学校我如何过滤它?

JSONObject json2 = new JSONObject(jsonresult);
status = json2.getString("status");
if (status.equals("1")) {
    message = "data";
    JSONArray school = json2.getJSONArray("data");
    for (int i = 0; i < school.length(); i++) {         
        JSONObject object = school.getJSONObject(i);
        if (object.getString("title").equals("Elementary Schools")){
            Category_ID.add(Long.parseLong(object.getString("school_id")));
            Category_name.add(object.getString("name"));
        }
    }
}

JSON响应:

{
  "status": 1,
  "data": [
    {
      "school_id": "321",
      "name": "Chavez",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "2",
      "id": "147",
      "level_id": "1",
      "title": "Elementary Schools"
    },
    {
      "school_id": "319",
      "name": "Central",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "2",
      "id": "145",
      "level_id": "1",
      "title": "Elementary Schools"
    },
    {
      "school_id": "318",
      "name": "Carver",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "2",
      "id": "144",
      "level_id": "1",
      "title": "Elementary Schools"
    },
    {
      "school_id": "317",
      "name": "Carson",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "2",
      "id": "143",
      "level_id": "1",
      "title": "Elementary Schools"
    },
    {
      "school_id": "314",
      "name": "Burbank",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "2",
      "id": "140",
      "level_id": "1",
      "title": "Elementary Schools"
    },
    {
      "school_id": "313",
      "name": "Boone",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "2",
      "id": "139",
      "level_id": "1",
      "title": "Elementary Schools"
    },
    {
      "school_id": "454",
      "name": "Preuss",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "1",
      "id": "280",
      "level_id": "2",
      "title": "Middle Schools"
    },
    {
      "school_id": "457",
      "name": "Riley",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "1",
      "id": "284",
      "level_id": "2",
      "title": "Middle Schools"
    },
    {
      "school_id": "478",
      "name": "Standley",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "1",
      "id": "304",
      "level_id": "2",
      "title": "Middle Schools"
    },
    {
      "school_id": "431",
      "name": "Muir",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "1",
      "id": "258",
      "level_id": "3",
      "title": "High Schools"
    },
    {
      "school_id": "454",
      "name": "Preuss",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "1",
      "id": "281",
      "level_id": "3",
      "title": "High Schools"
    },
    {
      "school_id": "466",
      "name": "San Diego",
      "phone": "",
      "email": "",
      "address": "",
      "information": "",
      "image": "",
      "calendar_id": "1",
      "id": "292",
      "level_id": "3",
      "title": "High Schools"
    }
  ]
}
4

3 回答 3

1
String title = object.getString("title");
if(title.equals("Elementary Schools"))
{
   //do your code
}

title.equalsIgnoreCase("Elementary Schools");你也可以使用

于 2013-10-14T11:30:01.930 回答
1

做#

if (object.getString("title").equalsIgnoreCase("小学")

反而#

if (object.getString("title") =="小学")

于 2013-10-14T10:22:52.803 回答
0
JUst do 

String title = object.getString("title");
if(title.equals("Elementary Schools"))
{
// Do you stuff
}

you can also use
if (object.getString("title").equalsIgnoreCase("Elementary Schools")
{`enter code here
// do you stuff
}

// 我刚刚注意到你的 if 语句后的 (;) 删除它并测试你的代码。

于 2013-10-14T10:59:40.070 回答