Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

更新Alamofire后,responseString里的中文乱码。

这是抓包的json:

{
    "buyer": {
        "tmallMemberLevel": 0
    },
    "consumerProtection": {
        "items": [{
            "desc": "?y?·±£??£??ùò??a??",
            "title": "?ùò??a??",
            "type": 0
        }, {
            "desc": "3?D?ó??§?úí?????3?oó£??íêü???ùí???μ???",
            "title": "???ùí???",
            "type": 0
        }, {
            "desc": "?ú×??àó|ì??tê±£???·????ééê?? ?°??ìì?Tàíóéí??????±",
            "title": "??ìì?Tàíóéí???",
            "type": 0
        }],
        "params": "204:3838,2979,2973",
        "passValue": "none",
        "serviceProtection": {
            "basicService": {
                "name": "?ù′?±£??",
                "services": [{
                    "desc": ["?y?·±£??£??ùò??a??"],
                    "icon": "//img.alicdn.com/tfs/TB1_YjSjeL2gK0jSZPhXXahvXXa-54-54.png",
                    "name": "?ùò??a??"
                }, {

使用 Alamofire 5.0 时,自动对 responseString 的乱码进行了处理:

{"buyer":{"tmallMemberLevel":0},"consumerProtection":{"items":[{"desc":"正品保障,假一赔四","title":"假一赔四","type":0},{"desc":"诚信用户在退货寄出后,享受极速退款到账","title":"极速退款","type":0},{"desc":"满足相应条件时,消费者可申请 “七天无理由退换货”","title":"七天无理由退换",

更新到 Alamofire 5.3,responseString:

{"buyer":{"tmallMemberLevel":0},"consumerProtection":{"items":[{"desc":"?y?·±£??£??ùò??a??","title":"?ùò??a??","type":0},{"desc":"3?D?ó??§?úí?????3?oó£??íêü???ùí???μ???","title":"???ùí???","type":0},{"desc":"?ú×??àó|ì??tê±£???·????ééê?? ?°??ìì?Tàíóéí??????±","title":"??ìì?Tàíóéí???"

如何在不回退的情况下快速处理这个问题?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

指定 responseString 的 encoding 为 GBK 编码,系统没有提供,可以自己加一个:

extension String.Encoding {
    
    public static var GB_18030_2000: String.Encoding {
        let cfEncoding = CFStringEncodings.GB_18030_2000
        let resEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(cfEncoding.rawValue))
        return String.Encoding(rawValue: resEncoding)
    }
    
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...