json数据转发
以map为例
public ActionForward getjson(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {
Map<String, String> resultmap = new HashMap<String, String>();
resultmap.put("status", "10005");
resultmap.put("message", "手机号或者用户名不能为空!");
JSONArray jsonArray = JSONArray.fromObject(resultmap);
String xml = DESCode.requestXml(jsonArray.toString());
this.writeAjaxResult(xml, response);
return null;
}
//ajax异步提交json
protected void writeAjaxResult(String s, HttpServletResponse response) {
try {
OutputStream os = response.getOutputStream();
os.write(s.getBytes("UTF-8"));
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
json数据解析
String url = "localhost:8080/getjson.jsp?id=123";
String result_login = TestHttp.sendPost(url);
if (!StringUtil.isNullOrBlank(result_login)){
JSONArray json_login = JSONArray.fromObject(result_login);
System.out.println("result_login="+result_login);
if(json_login!=null && json_login.size()>0){
Map result_Loginmap = (Map)json_login.getJSONObject(0);
String status = result_Loginmap.get("status")+"";
}
}