接口返回的 json数据中含有双引号 或其他非法字符的解决办法
2014-12-16 13:50
806 查看
最近做一个android新闻客户端 ,需要解析接口数据,格式json。 但是数据源中含有双引号 ,导致fastjson ,gson等都解析失败
如下:
"news" 第二条数据 ,titile值中带有双引号,破坏了json格式 ,导致无法解析
百度了下 ,没有找到合适的解决方案,又不能让接口方做调整,所以自己试着处理了下 ,把属性值中的英文双引号变换成中文的双引号
代码如下:
处理后顺利将属性值中的英文双引号变成中文双引号:
gson顺利解析成功 。
如果json数据中还包含其他不规范的符号 , 可以同时过滤掉 , 如下:
private static String jsonString(String s) {
char[] temp = s.toCharArray();
int n = temp.length;
for (int i = 0; i < n; i++) {
if (temp[i] == ':' && temp[i + 1] == '"') {
for (int j = i + 2; j < n; j++) {
if (temp[j] == '"') {
if (temp[j + 1] != ',' && temp[j + 1] != '}') {
temp[j] = '”';
} else if (temp[j + 1] == ',' || temp[j + 1] == '}') {
break;
}
} else if (temp[j] == '-') {
temp[j] = ' ';
} else if (true) {
// 要过虑其他字符,继续添加判断就可以
}
}
}
}
return new String(temp);
}
如下:
{"result":{"stat":"200","remark":"成功"}, "data":{"news":[ {"id":"104773","title":"黄湖一孤寡老人一次缴20000元党费"}, {"id":"112324","title":"余杭花费3亿回购28家卫生院 "医改"让百姓得到实惠"}, {"id":"112329","title":"余杭卫生院7年后纠错回购 7500万卖出3亿买回"}, {"id":"119324","title":"余杭黄湖镇3000多平方米厂房出租或转让"}, {"id":"119335","title":"余杭黄湖镇百丈镇一夜16辆车被撬"}, {"id":"407788","title":"黄湖镇召开城管工作会议"}, {"id":"407767","title":"普及急救知识 提高急救水平"}, {"id":"407775","title":"深化党内民主 完善自身建设 "}, {"id":"407793","title":"副区长阮英调研黄湖镇旅游产业发展和养老设施项目建设情况"} ] } }
"news" 第二条数据 ,titile值中带有双引号,破坏了json格式 ,导致无法解析
百度了下 ,没有找到合适的解决方案,又不能让接口方做调整,所以自己试着处理了下 ,把属性值中的英文双引号变换成中文的双引号
代码如下:
private static String jsonString(String oldJson) { char[] temp = oldJson.toCharArray(); int n = temp.length; for (int i = 0; i < n; i++) { if (temp[i] == ':' && temp[i + 1] == '"') { for (int j = i + 2; j < n; j++) { if (temp[j] == '"') { if (temp[j + 1] != ',' && temp[j + 1] != '}') { temp[j] = '”'; } else if (temp[j + 1] == ',' || temp[j + 1] == '}') { break; } } } } } return new String(temp); }
处理后顺利将属性值中的英文双引号变成中文双引号:
{"result":{"stat":"200","remark":"成功"}, "data":{"news":[ {"id":"104773","title":"黄湖一孤寡老人一次缴20000元党费"}, {"id":"112324","title":"余杭花费3亿回购28家卫生院 “医改”让百姓得到实惠"}, {"id":"112329","title":"余杭卫生院7年后纠错回购 7500万卖出3亿买回"}, {"id":"119324","title":"余杭黄湖镇3000多平方米厂房出租或转让"}, {"id":"119335","title":"余杭黄湖镇百丈镇一夜16辆车被撬"}, {"id":"407788","title":"黄湖镇召开城管工作会议"}, {"id":"407767","title":"普及急救知识 提高急救水平"}, {"id":"407775","title":"深化党内民主 完善自身建设 "}, {"id":"407793","title":"副区长阮英调研黄湖镇旅游产业发展和养老设施项目建设情况"} ] } }
gson顺利解析成功 。
如果json数据中还包含其他不规范的符号 , 可以同时过滤掉 , 如下:
private static String jsonString(String s) {
char[] temp = s.toCharArray();
int n = temp.length;
for (int i = 0; i < n; i++) {
if (temp[i] == ':' && temp[i + 1] == '"') {
for (int j = i + 2; j < n; j++) {
if (temp[j] == '"') {
if (temp[j + 1] != ',' && temp[j + 1] != '}') {
temp[j] = '”';
} else if (temp[j + 1] == ',' || temp[j + 1] == '}') {
break;
}
} else if (temp[j] == '-') {
temp[j] = ' ';
} else if (true) {
// 要过虑其他字符,继续添加判断就可以
}
}
}
}
return new String(temp);
}
相关文章推荐
- Gson解析Json数据内有非法字符("",回车,etc)时的解决办法
- Jquery form.js文件上传返回JSON数据,在IE下提示下载文件的解决办法,并对返回数据进行正确的解析
- 三大框架整合后Struts2返回Json数据会出现的一些错误以及解决办法
- 解决在IE浏览器中返回json格式的数据时提示下载办法
- java读取 其他服务接口 返回的json数据
- C# 调用C++ DLL 返回类型为字符的接口 出现异常 解决办法
- 动网:关于部分论坛用户发帖、回帖时报错“数据中含有非法字符。您的用户名并不存在,或者您的论坛密码错误,或者您的帐号已被管理员锁定。” 及“您没有发表新主题的权限”解决方法
- Spring MVC避免IE提示下载返回的JSON 数据——解决办法
- 解决C#后台返回json数据双引号转义为"问题
- 调用WebAPI接口地址返回序列化的JSON对象中的属性都加了个k__BackingField关键字的解决办法
- springmvc返回json数据406错误的解决办法
- ubuntu使用命令mongoimport插入大批量json数据到mongodb时,遇到转义字符出错解决办法
- java读取其他服务接口返回的json数据示例代码
- 解决在IE浏览器中返回json格式的数据时提示下载办法
- owin 返回json字符串多带了双引号"多了重string转义字符串解决办法
- IOS 返回JSON数据中存在NULL的解决办法
- Discuz!您当前的访问请求当中含有非法字符,已经被系统拒绝解决办法
- jQuery在异步请求数据返回后,调用$("selector").html(data.content);之后因为一些特殊字符或者',"不能显示内容的问题解决办法
- 用struts2开发的App后台,返回json数据中文为问号的解决办法
- Silverlight在调用wcf时传输数据过大返回Not Found的解决办法