2016-09-20 18 views
0

我想過濾每個性別的記錄,我以JSON響應的形式收到記錄。我有一個從我的DAO類返回Json數據的方法,所以我想解析它的內容,這樣我可以與性別類型如何在java servlet上過濾JSON結果中的記錄

String results = bedsBean.allBedsInJson(); 
System.out.println(results); 

濾波器提供記錄貝婁

[{"bedNo": "1", "bedType": "Upper", "roomNo": "1", "roomType": "4500.0", "roomType": "Quadriple", "blockId": "MMA001", "hostelName": "MAMANGINA", "gender": "Female", "status": "VACANT", "id": "3"}] 

我想這樣的列表,但它不可能工作

FileReader reader = new FileReader(results); 
    JSONParser jsonParser = new JSONParser(); 
    JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); 
    // get a String from the JSON object 
    String bedNo = (String) jsonObject.get("bedNo"); 
    System.out.println("The first name is: " +bedNo); 
    //....................... 

回答

0

嘗試這樣做。

JSONObject jsonObject = new JSONObject(results); 
String bedno = jsonObject.getString("bedNo"); 
String gender = jsonObject.getString("gender"); 
0

您可以嘗試使用GSon:https://github.com/google/gson。 創建一個DTO:

public class ObjectDTO() { 
    private String bedNo; 
    private String gender; 
} 

做:

new Gson().fromJson(yourJsonStringHere, ObjectDTO.class);