您的位置:首页 > Web前端 > JavaScript

js去除空字符方法和一个解决json乱码的方法

2012-03-22 13:50 726 查看
function myTrim(s)

{

//去掉前面空格  aaa

var head=s.substring(0,1);

while(head==" ")

{

  s=s.replace(" ","");

  head=s.substring(0,1);

}

//去掉后面空格

var len=s.length;

var end=s.substring(len-1,len);

while(end==" ")

{

  len--;

  end=s.substring(len-1,len);

}

s=s.substring(0,len);

return s;

}
java后台接收字符为乱码
request设置编码
  jsons =  java.net.URLDecoder.decode(java.net.URLDecoder.decode(jsons , "utf-8"), "utf-8");//这个可以解决%与字母的乱码

  @RequestMapping("show.do")

   public void showCjxxByCjId(HttpServletRequest request,

     HttpServletResponse response)throws IOException{

    response.setContentType("application/json;charset=utf-8");

     response.setHeader("pragma", "no-cache");

  response.setHeader("cache-control", "no-cache");  //这三个response 设置响应值为json格式

    String cjId =request.getParameter("cjId");

    try {

     TpCjxx cjxx = tpcjxxService.geTpCjxx(cjId);

    

   JsonConfig config = new JsonConfig();

   config.setJsonPropertyFilter(new PropertyFilter(){

    public boolean apply(Object source, String name, Object value) {

     if(name.equals("tpNltjZtxx")) { //将tpNltjZtxx过滤掉  对象转换json对象时对象里面的对象是不能转换成功的,所以必须过滤掉,否则转换不能成功

        return true;

     } else {

        return false;

     }

    }

   });

     JSONObject object = JSONObject.fromObject(cjxx,config);//直接将java对象转换json对象

    

     PrintWriter out = response.getWriter();

              out.print(object.toString());

              out.flush();

              out.close();

} catch (Exception e) {

  // TODO: handle exception

  log.error(e.getMessage());

}

   

   }

http://www.iteye.com/topic/1121623 K线图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐