private void getJsonData(HttpServletRequest request,HttpServletResponse response, Map result)
{
DevLog.info("in .getJsonData() json start ");
String callback = request.getParameter("callback");
response.setContentType("application/json;charset=utf-8");
PrintWriter out = null;
if(result == null)
{
try
{
String jsonStr = "";
out = response.getWriter();
out.println(jsonStr);
DevLog.info("in .getJsonData() json data ========================= "+ jsonStr);
}
catch (Exception e)
{
DevLog.error("in .getJsonData() json error ",e);
}
finally
{
if (null != out)
{
out.close();
}
DevLog.info("in .getJsonData() json end ");
}
}
try
{
JSONObject json = JSONObject.fromObject(result);
String jsonStr = json.toString();
if(!isEmpty(callback))
{
jsonStr = callback + "("+ jsonStr +")";
}
DevLog.info("in .getJsonData() json data ======================= "+ jsonStr );
out = response.getWriter();
out.println(jsonStr);
}
catch (Exception e)
{
DevLog.error("in .getJsonData() json error ",e);
}
finally
{
if (null != out)
{
out.close();
}
DevLog.info("in .getJsonData() json end ");
}
}
{
DevLog.info("in .getJsonData() json start ");
String callback = request.getParameter("callback");
response.setContentType("application/json;charset=utf-8");
PrintWriter out = null;
if(result == null)
{
try
{
String jsonStr = "";
out = response.getWriter();
out.println(jsonStr);
DevLog.info("in .getJsonData() json data ========================= "+ jsonStr);
}
catch (Exception e)
{
DevLog.error("in .getJsonData() json error ",e);
}
finally
{
if (null != out)
{
out.close();
}
DevLog.info("in .getJsonData() json end ");
}
}
try
{
JSONObject json = JSONObject.fromObject(result);
String jsonStr = json.toString();
if(!isEmpty(callback))
{
jsonStr = callback + "("+ jsonStr +")";
}
DevLog.info("in .getJsonData() json data ======================= "+ jsonStr );
out = response.getWriter();
out.println(jsonStr);
}
catch (Exception e)
{
DevLog.error("in .getJsonData() json error ",e);
}
finally
{
if (null != out)
{
out.close();
}
DevLog.info("in .getJsonData() json end ");
}
}
本文介绍了一个用于处理HTTP请求中JSON数据的方法,通过分析请求参数并返回相应的JSON数据或JSONP格式的数据。该方法首先检查传入的Map是否为空,若为空则直接返回空字符串;若有数据,则将Map转换为JSON格式,并支持JSONP回调。
2184

被折叠的 条评论
为什么被折叠?



