假设您使用的是HttpServlet和类似json-
simple的JSON库,则可以执行以下 *** 作:
public JSonObject requestParamsToJSON(ServletRequest req) { JSonObject jsonObj = new JSonObject(); Map<String,String[]> params = req.getParameterMap(); for (Map.Entry<String,String[]> entry : params.entrySet()) { String v[] = entry.getValue(); Object o = (v.length == 1) ? v[0] : v; jsonObj.put(entry.getKey(), o); } return jsonObj;}
public void doPost(HttpServletRequest req, HttpServletResponse res) { JSonObject jsonObj = requestParamsToJSON(req); // Now "jsonObj" is populated with the request parameters. // e.g. {"key1":"value1", "key2":["value2a", "value2b"], ...}}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)