在body中传值,以HttpServletRequest接收值,数据处理方法

json串:

{

    "fullPrice": 115,

    "tickets": [{

            "appointment_period_ids": "290ede6f1f2c423cb2dc4ec6826c9a02",

                        "customers":[{

                                     "userIds":"2ca6db6c7d3b4e7a882ceb600a96b59f",

                                     "name":"徐元文",

                                     "tel":"17664523248",

                                     "useDate":"2020-09-21",

                                     "idCard":"370983199407022338"

                        }],                            

        }

    ]

}

 

接收方法:

public static String readData(HttpServletRequest request) {
   BufferedReader br = null;
   try {
      StringBuilder result = new StringBuilder();
      br = request.getReader();
      for (String line=null; (line=br.readLine())!=null;) {
         result.append(line).append("\n");
      }
      
      return result.toString();
   } catch (IOException e) {
      throw new RuntimeException(e);
   }
   finally {
      if (br != null)
         try {br.close();} catch (IOException e) {LogKit.error(e.getMessage(), e);}
   }
}

数据处理方法:

JSONArray ticketsList = requestObject.getJSONArray("tickets");
if(ticketsList.size()>0){
    JSONObject jsonObject = ticketsList.getJSONObject(0);
    String ticketIds = jsonObject.getString("ticketIds");
    JSONArray customers = jsonObject.getJSONArray("customers");
    if(customers.size()>0){
        JSONObject customersJsonObject = customers.getJSONObject(0);
        useDate = customersJsonObject.getString("useDate");
    }
    Ticket ticket = TicketService.service.getTicketInfo(ticketIds);
    Integer  isweekendprice = ticket.getIsweekendprice();
    if(isweekendprice==1){
       Boolean isWeek =  isWeekend(useDate);
       if(!isWeek){
           log.error("票信息异常:使用日期不是周末");
           throw new RuntimeException("票信息异常:使用日期不是周末");
       }
    }

 

简单来说,碰到 [ ] (中括号)用JSONArray,获取一条数据{ } 里面的某个字段的值使用

  JSONObject jsonObject = ticketsList.getJSONObject(0);

  String ticketIds = jsonObject.getString("ticketIds");

你可能感兴趣的:(JAVA)