如何从资产中过滤文本文件以仅显示特定结果

时间:2013-10-12 08:57:01

标签: android

我从资源加载文本文件并在listview中显示使用json在我的文本文件somany学校我粘贴我的file_name.json下面的问题是我想过滤只显示特定学校名称是"Middle Schools"我如何过滤这个第JSONObject json2 = new JSONObject(jsonresult);行只显示名为"MiddleSchool"

的学校
 public String loadJSONFromAsset() {
    String json = null;
    try {

        InputStream is = getAssets().open("file_name.json");

        int size = is.available();

        byte[] buffer = new byte[size];

        is.read(buffer);

        is.close();

        json = new String(buffer, "UTF-8");
        jsonresult=json;

    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}
                    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);

                Category_ID.add(Long.parseLong(object
                        .getString("school_id")));
                Category_name.add(object.getString("name"));
            }
        }
        else {
                message = "error";
        }
        }

文字档案:

     {"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":"316","name":"Cadman","phone":"","email":"",
 "address":"","information":"","image":"","calendar_id":"1",
 "id":"142","level_id":"1","title":"Elementary Schools"},

{"school_id":"315","name":"Cabrillo","phone":"","email":"",
 "address":"","information":"","image":"","calendar_id":"1",
 "id":"141","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":"498","name":"Zamorano","phone":"","email":"",
"address":"","information":"","image":"","calendar_id":"2",
"id":"324","level_id":"1","title":"Elementary Schools"},

  {"school_id":"451","name":"Pershing","phone":"","email":"",
  "address":"","information":"","image":"","calendar_id":"1",
  "id":"277","level_id":"2","title":"Middle 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":"462","name":"Roosevelt","phone":"","email":"",
 "address":"","information":"","image":"","calendar_id":"1",
"id":"288","level_id":"2","title":"Middle Schools"},

 {"school_id":"468","name":"SCPA","phone":"","email":"",
"address":"","information":"","image":"","calendar_id":"1",
"id":"294","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":"439","name":"O'Farrell","phone":"","email":"",
 "address":"","information":"","image":"","calendar_id":"1",
"id":"267","level_id":"3","title":"High Schools"},


 {"school_id":"452","name":"Point Loma","phone":"","email":"",
 "address":"","information":"","image":"","calendar_id":"1",
"id":"278","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"}]}

1 个答案:

答案 0 :(得分:1)

您遍历JSON对象,直到您拥有正确的项目。这没有内置功能。您可以在API中找到所有必需的方法。

我不会使用JSONObject。使用带注释的Jackson要容易得多。