使用Jackson的BeanPropertyFilter过滤对象列表

时间:2015-09-02 04:28:17

标签: java json filter jackson

我在主类中有了objectList。我正在使用Jackson的SimpleBeanPropertyFilter.filterOutAllExcept,但我没有得到过滤后的响应。任何人都可以帮助我,JSON响应如下。

{
   "person":    [
            {
         "firstName": "Shyam",
         "age": 7,
         "addressDetails":          [
                        {
               "houseNo": "1236",
               "buildingNo": "T4",
               "address1": "sector 34",
               "address2": "near hospital",
               "city": "noida",
               "state": "UP",
               "pinCode": 1400021
            },
                        {
               "houseNo": "1236",
               "buildingNo": "T4",
               "address1": "sector 34",
               "address2": "near hospital",
               "city": "noida",
               "state": "UP",
               "pinCode": 1400021
            }
         ],
         "lastName": "singh"
      }
   ],
   "syncDate": 1441167154507,
   "nextPage": "http://localhost:8080/personData/Date=1437458428657"
}

1 个答案:

答案 0 :(得分:0)

尝试这个,使用两个循环1st用于获取人员数组数据,其他用于j用于获取地址详细信息数据。

try {
        JSONObject responseJsonObject = new JSONObject(response);
        String syncDate=responseJsonObject.getString("syncDate");
        String nextPage=responseJsonObject.getString("nextPage");

        JSONArray jsonPerson_jsonarray = responseJsonObject
                .getJSONArray("person");
        Log.i("get_array", jsonPerson_jsonarray + "");
        for (int i = 0; i < jsonPerson_jsonarray.length(); i++) {
            JSONObject personDetail = jsonPerson_jsonarray
                    .getJSONObject(i);

            String firstName=personDetail.getString("firstName");
            String age=personDetail.getString("age");
            String lastName=personDetail.getString("lastName");
            JSONArray addressDetails_jsonarray = personDetail
                    .getJSONArray("addressDetails");

            for (int j = 0; j < addressDetails_jsonarray.length(); j++) {
                JSONObject addressDetails = addressDetails_jsonarray
                        .getJSONObject(j);

                String houseNo=addressDetails.getString("houseNo");
                String buildingNo=addressDetails.getString("buildingNo");
                String address1=addressDetails.getString("address1");
                String address2=addressDetails.getString("address2");
                String city=addressDetails.getString("city");
                String state=addressDetails.getString("state");
                String pinCode=addressDetails.getString("pinCode");
            }

        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

用bean类getter setter方法替换循环字符串或使用数组,并获取所需的数据