java中如何根据HttpServletRequest获取JSON中POST数据呢?
下文笔者讲述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");
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


