iOS Swift URL转义网络请求json参数含有中文/不规

2021-12-21  本文已影响0人  Lee坚武

在本地开发过程中,服务端需要的参数因为含有json,json里面参数还有中文字符:如下面

{\"game_id\":{\"value\":\"19\",\"title\":\"游戏ID\"},\"zone_name\":{\"value\":\"CP区名称\",\"title\":\"区服名称\"},\"role_name\":{\"value\":\"CP角色名\",\"title\":\"角色名称\"},\"role_id\":{\"title\":\"角色ID\",\"value\":\"CP角色ID\"},\"system\":{\"title\":\"系统\",\"value\":\"iOS\"},\"zone_id\":{\"title\":\"区ID\",\"value\":\"11111\"}}
image.png
这样子是会请求的时候报nil的,导致崩溃,需要对他进行再一次转换
解决办法:
let imMetaInfo = UserDefaults.standard.object(forKey: "im_meta")
let toSearchword = CFURLCreateStringByAddingPercentEscapes(nil, (imMetaInfo as! CFString), nil, "!*'();:@&=+$,/?%#[]" as CFString, CFStringBuiltInEncodings.UTF8.rawValue)

这样子的就可以了

%7B%22game_id%22%3A%7B%22value%22%3A%2219%22%2C%22title%22%3A%22%E6%B8%B8%E6%88%8FID%22%7D%2C%22zone_name%22%3A%7B%22value%22%3A%22CP%E5%8C%BA%E5%90%8D%E7%A7%B0%22%2C%22title%22%3A%22%E5%8C%BA%E6%9C%8D%E5%90%8D%E7%A7%B0%22%7D%2C%22role_name%22%3A%7B%22value%22%3A%22CP%E8%A7%92%E8%89%B2%E5%90%8D%22%2C%22title%22%3A%22%E8%A7%92%E8%89%B2%E5%90%8D%E7%A7%B0%22%7D%2C%22role_id%22%3A%7B%22title%22%3A%22%E8%A7%92%E8%89%B2ID%22%2C%22value%22%3A%22CP%E8%A7%92%E8%89%B2ID%22%7D%2C%22system%22%3A%7B%22title%22%3A%22%E7%B3%BB%E7%BB%9F%22%2C%22value%22%3A%22iOS%22%7D%2C%22zone_id%22%3A%7B%22title%22%3A%22%E5%8C%BAID%22%2C%22value%22%3A%2211111%22%7D%7D
image.png
更多方法交流可以家魏鑫:lixiaowu1129,一起探讨iOS相关技术!
上一篇下一篇

猜你喜欢

热点阅读