IOS 中 urldecode 如何解码后,解码后“+”(加号)和空格处理方式
请求后的结果如下:
{ "IsSuccess": 1, "ResCode": "1", "ResMsg": "成功", "Data": { "ID": "249", "Type": "1", "Title": "物业福利大放送", "Second_Title": "物业福利", "Event_Date": "", "Main_Pic": "!UploadFiles%5cWB_Content_Pic%5c20151013152614459.jpg", "Writer": "物业管理处", "Info": "%3cp+class%3d%22MsoNormal%22% 这里省略了20KB....", "FromWhere": "蓝堡国际", "Hit_Count": "1" } }
这里看到的 Info
下是一个较大的HTML富文本值,里面各种符号,也有特殊符号,但是采用的传输格式是JSON,所以采用了 url encode
加密,然后 decode
解密 。
在Java
、Android
解析都能decode
正常的解析过来。
Base64 base64 = new Base64(); //org.apache.xmlbeans.impl.util.Base64 byte[] bytes = base64.encode(s.getBytes("UTF-8")); String base64Str = new String(bytes, "UTF-8"); result.put("info", base64Str); System.out.println("\n\n\n"); System.out.println(base64Str); byte[] decodebyte = base64.decode(base64Str.getBytes()); String decodeStr = new String(decodebyte, "UTF-8"); System.out.println(decodeStr); //解析后不乱码,原来的空格也恢复成空格。
在IOS环境,业红
(备注:业红是个人名)后来想到一个大招。
首先把接收到的信息,replace
把“+”替换成 “ ” ,然后再decode
。说到这里,有人会说了,那如果本来就有(“+”)加号呢?加号encode
后不是空格,也不是加号,而是%2b
,正常情况下,空格是%20
,那也就是说,如果原文中有加号,是不会正常显示的,也就是在接收到信息的时候,先replace
,然后再decode
,这样就解决了。
代码如下:
//获取字符串 NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; //替换+ 为空格 content = [content stringByReplacingOccurrencesOfString:@"+" withString:@" "]; //decode content = [content stringByRemovingPercentEncoding]; NSLog(@"%@",content);
希望有完美解决方案的,在下面给出完美解决方案。
转载请注明出处:https://www.sojson.com/blog/6.html
版权所属:SO JSON在线解析
原文地址:https://www.sojson.com/blog/6.html
转载时必须以链接形式注明原始出处及本声明。
如果本文对你有帮助,那么请你赞助我,让我更有激情的写下去,帮助更多的人。