jsonp 常用util方法
编程技术  /  houtizong 发布于 3年前   235
jsonp 常用java方法
(1)以jsonp的形式返回:函数名(json字符串)
/*** * 用于jsonp调用 * @param map : 用于构造json数据 * @param callback : 回调的javascript方法名 * @param filters : <code>SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("content");FilterProvider filters = new SimpleFilterProvider().addFilter(Constant2.SIMPLEFILTER_JACKSON_PAPERNEWS, theFilter);</code> * @return : js函数名(json字符串) */public static String getJsonP(Object map,String callback,FilterProvider filters){if(ValueWidget.isNullOrEmpty(map)){return null;}String content = null;if(map instanceof String){content=(String)map;}else{ObjectMapper mapper = getObjectMapper();ObjectWriter writer=null;try {if(filters!=null){writer=mapper.writer(filters);}else{writer=mapper.writer();}content=writer.writeValueAsString(map);logger.info(content);} catch (JsonGenerationException e) {e.printStackTrace();} catch (JsonMappingException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}if(ValueWidget.isNullOrEmpty(callback)){return content;}return callback+"("+content+");";}
应用:
@ResponseBody@RequestMapping(value = "/json_detail", produces = SystemHWUtil.RESPONSE_CONTENTTYPE_JSON_UTF)public String jsonDetail(Model model, int id/*新闻的id*/,PaperNewsView view, HttpSession session,HttpServletRequest request, String callback) throws IOException {init(request);PaperNews news=(PaperNews) getDao().get(id);String content;/*int comment_type=0;if(type==2){//新闻comment_type=Constant2.COMMENT_TARGET_TYPE_NEWS;}else if(type==1){}*/String title=null;if(news.getType()==Constant2.TYPE_NEWS){title="新闻";}else{title="报料";}long commentCount=this.newsCommentDao.getCount(null, id, 1);news.setCommentSum(commentCount);if(!ValueWidget.isNullOrEmpty(news.getPic())){news.setPic(JSONPUtil.getPicPath(news.getPic()));}content = getJsonP(news, callback);AccessLog accessLog=logInto(request);accessLog.setDescription("手机端"+title+"详情,id:"+id);logSave(accessLog, request);return content;}
(2)去掉callback
callback({"auth_code":"v39hXq"}) -->{"auth_code":"v39hXq"}
/*** * callback({"auth_code":"v39hXq"}) -->{"auth_code":"v39hXq"} * @param input * @param callbackName * @return */public static String deleteCallback(String input,String callbackName){return input.replaceAll("^"+callbackName+"\\((.*\\})\\);?$", "$1");}
应用:
/*** * convert json string to Map;e.g:{errorMessage=系统异常,请稍后再试!, id=, status=02, errorCode=error_default, method=} * @param jsonResult * @return * @throws UnsupportedEncodingException * @throws JSONException * @throws org.json.JSONException */public static Map<String, String> getMap(String jsonResult)throws UnsupportedEncodingException, JSONException,org.json.JSONException {if(ValueWidget.isNullOrEmpty(jsonResult)){return null;}//callback({"auth_code":"v39hXq"})jsonResult=deleteCallback(jsonResult, "callback");Map<String, String> resultMap =null;Map obj = (Map) JsonParser.parserRandomJsonFormat(jsonResult);if (ValueWidget.isNullOrEmpty(obj)) {return null;}List resultList = (List) obj.get("resultList");if(ValueWidget.isNullOrEmpty(resultList)){resultMap=obj;}else{resultMap= new HashMap<String, String>();for (int i = 0; i < resultList.size(); i++) {Map mp_tmp = (Map) resultList.get(i);parseMap(resultMap, mp_tmp);}}return resultMap;}
参考:http://www.cnblogs.com/quanyongan/archive/2013/04/16/3024993.html
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接