servlet中将接收的参数转成Json

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                public static String readJSONString(HttpServletRequest request) {
StringBuffer json = new StringBuffer();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {
json.append(line);
}
if(json.length()<1){
Map<String, String[]> hm=request.getParameterMap();
if (hm!=null && hm.size()>0){
json.append(readjson(hm).toString());
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
return json.toString();
}

public static JSONObject readjson(Map<String, String[]> hm){
        JSONObject jobj = new JSONObject();
//通过循环遍历的方式获得key和value并set到JSONObject中
Iterator it = hm.keySet().iterator();
while (it.hasNext()) {
       String key = it.next().toString();
       String[] values = (String[])hm.get(key);
       jobj.put(key, values[0]);
}
        return jobj;
}

public static JSONObject readjson(HttpServletRequest request){
        JSONObject JSONObject = new JSONObject();
        Map pmap = request.getParameterMap();
//通过循环遍历的方式获得key和value并set到jsonobject中
Iterator it = pmap.keySet().iterator();
while (it.hasNext()) {
       String key = it.next().toString();
       String[] values = (String[])pmap.get(key);
       JSONObject.put(key, values[0]);
}
        return JSONObject;
}            

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

猜你喜欢

转载自blog.csdn.net/hhduyc/article/details/84143371