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线图
{
//去掉前面空格 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线图
相关文章推荐
- java socket通信时,中文字符乱码的一个解决方法
- thrift的js客户端收到含汉字字符中显示为乱码解决方法
- js replace 全局替换 以表单的方式提交参数 判断是否为ie浏览器 将jquery.qqFace.js表情转换成微信的字符码 手机端省市区联动 新字体引用本地运行可以获得,放到服务器上报404 C#提取html中的汉字 MVC几种找不到资源的解决方式 使用Windows服务定时去执行一个方法的三种方式
- JSON格式的特殊字符处理(只要一个方法解决)
- 处理字符串的一些js/jq方法(去除HTML,去除空格,计算真实长度,截取中英文字符)
- VC轻松解析XML文件--CMarkup使用方法(解决解析中文字符出现乱码问题)
- IE浏览器用GET方式传递中文字符出现乱码问题的解决方法
- 从js向Action传中文参数出现乱码问题的解决方法
- 黑马day03 字符流乱码解决方法以及原理分析
- JS——实现一个删除字符串左边空白字符的方法
- 解决3.01版的jquery.form.js中文乱码问题的解决方法
- js向java后台传汉字乱码的解决方法
- struts2中通过json传值解决乱码问题的实现方法
- Servlet 使用字符流读取文件乱码解决方法
- Android读取本地json文件的方法(解决显示乱码问题)
- 页面js遇到乱码问题的解决方法是和无法转码的情况
- Servlet从上一个页面获取参数(中文乱码解决方法)
- Oracle中将数字转换成字符插入表中会多一个空格的解决方法
- PHP json_encode中文乱码解决方法
- JS中的中文在jsp页面中乱码的解决方法