简体   繁体   English

如何在Java Servlet上过滤JSON结果中的记录

[英]How to filter Records in a JSON results on java servlet

I am trying to filter records per gender that i received in form of a JSON response.I have a methord from my DAO class that returns a Json Data so i Would like to parse its contents so that i can filter with Gender type 我正在尝试过滤以JSON响应形式收到的每个性别的记录。我有一个来自DAO类的方法,该方法返回了一个Json数据,因此我想解析其内容以便可以按性别类型进行过滤

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

gives a List of Records Bellow 给出记录列表

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

I tried this but It couldnt work 我试过了但是没用

  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);
  //.......................

Try doing this. 尝试这样做。

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

You could try to use GSon: https://github.com/google/gson . 您可以尝试使用GSon: https : //github.com/google/gson Create a DTO: 创建一个DTO:

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

and do: 并做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM