2016-09-20 2 views
0

JSON 응답 형식으로받은 성별 레코드를 필터링하려고합니다. JSON 데이터를 반환하는 DAO 클래스의 methord가 있으므로 마음에 들었습니다. 내가 성별 유형필터링하는 방법 자바 서블릿에서 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); 
    //....................... 
,369을 couldnt한다

답변

0

이렇게하십시오.

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

GSon : https://github.com/google/gson을 사용해보세요.

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

및 수행 : DTO를 만들기

new Gson().fromJson(yourJsonStringHere, ObjectDTO.class); 
관련 문제