java中如何根据HttpServletRequest获取JSON中POST数据呢?

戚薇 Java经验 发布时间:2023-04-23 22:13:51 阅读数:17549 1
下文笔者讲述HttpServletRequest获取POST中JSON数据的方法分享,如下所示

HttpServletRequest获取POST中JSON数据的实现思路

使用request.getReader();
    即可获取前端传送过来的json信息
例:使用post请求参数
//post参数
  {"jsondata":"data"}


//doPost获取post参数信息

public void doPost(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {

  StringBuffer jb = new StringBuffer();
  String line = null;
  try {
    BufferedReader reader = request.getReader();
    while ((line = reader.readLine()) != null)
      jb.append(line);
  } catch (Exception e) { /*report an error*/ }

  try {
    JSONObject jsonObject =  HTTP.toJSONObject(jb.toString());
  } catch (JSONException e) {
    // crash and burn
    throw new IOException("Error parsing JSON request string");
  }
}
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202304/16822593036300.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者