获取前端post方式传过来的JSON格式的数据的代码

来源:互联网 发布:02795522网络诈骗电话 编辑:程序博客网 时间:2024/05/21 17:56
public JSONObject getRequestContent(HttpServletRequest req) {JSONObject data = null;try {InputStream is = req.getInputStream();BufferedReader br = new BufferedReader(new InputStreamReader(is, "utf-8"));String line = null;StringBuffer content = new StringBuffer();while ((line = br.readLine()) != null) {System.out.println(line);content.append(line);System.out.println(content);}String reqStr = content.toString().trim();if (StringUtils.isEmpty(reqStr)) {return new JSONObject();}if (reqStr.contains("=")) {reqStr = reqStr.replaceAll("=", ":");}if (!reqStr.startsWith("{")) {reqStr = "{" + reqStr;}if (!reqStr.endsWith("}")) {reqStr = reqStr + "}";}data = JSONObject.fromObject(reqStr);} catch (Exception e) {e.printStackTrace();return new JSONObject();}return data;}
0 0
原创粉丝点击