美文网首页
Swift 解析的json中unicode的问题(\u)

Swift 解析的json中unicode的问题(\u)

作者: 字节码 | 来源:发表于2020-03-31 22:31 被阅读0次

    Swift 无法解析的json中unicode的问题(\u)

    使用Foundation的JSONSerialization解析带有unicode的json 字符串时,报错如下:

    Printing description of error:
    Error Domain=NSCocoaErrorDomain Code=3840 "Unexpected end of file during string parse (expected low-surrogate code point but did not find one)." UserInfo={NSDebugDescription=Unexpected end of file during string parse (expected low-surrogate code point but did not find one).}
    

    解析出错的json如下:

    {
       "zhid": null,
       "orderTime": "4小时前",
       "nickName": "仙***\uDF19",
       "userAvatar": "http://auth2.xunbaotianxing.com/authimg/avatar/2202222685.png",
       "orderNum": 191159,
       "orderDesc": "【新鲜水果】【黄金瓜】 x1.0份 ¥17.55份(1个/份,约2-2.5斤)\n【新鲜水果】【红颜草莓】 x4.0份 ¥16.0份(400g/份)\n【新鲜水果】【小台芒】 x2.0份 ¥7.8份(1斤/份)\n【新鲜水果】【沃柑散装】 x3.0份 ¥7.5份(1斤/份)"
      }
    

    仙***\uDF19这段文本中有unicode 编码,无法正常解析,

    解决方案:
    CFStringTransform 可以解决转换unicode 编码, 其可以在拉丁语和阿拉伯语、西里尔语、希腊语、韩语(韩国)、希伯来语、日语(平假名和片假名)、普通话、泰语之间来回转写。
    通过其进行转换

    func unicodeString(_ str: String) -> String {
        let mutableStr = NSMutableString(string: str) as CFMutableString
        CFStringTransform(mutableStr, nil, "Any-Hex/Java" as CFString, true)
        return mutableStr as String
    }
    

    将json 字符串 通过CoreFoundation中的CFStringTransform 转码,然再通过JSONSerialization解析:

    func test() {
        do {
            let data = try Data.init(contentsOf: URL(fileURLWithPath: "jsontest.json"), options: .alwaysMapped)
            if let str = String(data: data, encoding: .utf8), let data = str.data(using: .utf8)
            {
                do {
                    let obj = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed)
                    print(obj)
                } catch {
                    print("error")
                }
            }
            
        } catch  {
            print("error")
        }
    }
    

    或者使用swift标准库的String的方法:

    @available(OSX 10.11, iOS 9.0, *)
        public func applyingTransform(_ transform: StringTransform, reverse: Bool) -> String?
    

    解析:

    func test() {
        do {
            let data = try Data.init(contentsOf: URL(fileURLWithPath: "jsontest.json"), options: .alwaysMapped)
            if let str = String(data: data, encoding: .utf8)
            {
                guard let transformStr = str.applyingTransform(StringTransform(rawValue: "Any-Hex/Java"), reverse: true) else {
                    return
                }
                let data = transformStr.data(using: .utf8)
                do {
                    let obj = try JSONSerialization.jsonObject(with: data!, options: .fragmentsAllowed)
                    print(obj)
                } catch {
                    print("error")
                }
            }
            
        } catch  {
            print("error")
        }
    }
    

    相关文章

      网友评论

          本文标题:Swift 解析的json中unicode的问题(\u)

          本文链接:https://www.haomeiwen.com/subject/bncnuhtx.html