简体   繁体   English

带有Swift 3和Alamofire的JSON字符串上的Unicode字符

[英]Unicode characters on JSON string with swift 3 and Alamofire

I'm dealing with this issue here. 我在这里处理这个问题。 I have make a json request with alamofire and inside my json there are some characters like u00b etc. Which is greek language. 我已经用alamofire发出了json请求,并且在json内有一些字符,例如u00b等。这是希腊语言。 Although when i print the dictionary i cant see the correct string like "αβγ" but their unicode characters. 虽然当我打印字典时,我看不到正确的字符串,例如“αβγ”,但它们的Unicode字符。

Which is the proper way with alamofire to unicode all the text that i get to utf-8 so i can print out the proper characters?? 用alamofire正确编码所有我到utf-8的文本的正确方法是什么,以便我可以打印出正确的字符?

The following code is the request that i make. 以下代码是我提出的要求。

func getManPerf() {
    Alamofire.request(baseurl, method: .get, parameters: ["action": "categories", "subaction": "getproducts", "category_id": "11", "limit": "0,30"]).responseJSON { (responseData) -> Void in
        if((responseData.result.value) != nil) {
            let result = responseData.result
            print(result)

            if let dict = result.value as? Dictionary<String, AnyObject>{
                if let list = dict["products_in_category"] as? [Dictionary<String, AnyObject>] {

                    for obj in list {
                        let manPerfumes = Products(productDict: obj)
                        self.manPerfumeData.append(manPerfumes)
                    }
                    DispatchQueue.main.async{
                        self.manPerfumeCV.reloadData()

                    }


                }
            }

        }
    }
}

What changes do i have to make to my code so it can bring me the correct data? 我必须对代码进行哪些更改,才能为我带来正确的数据? Thanks! 谢谢!

Format the string with UTF-8 before appending ;) 在附加;)之前使用UTF-8格式化字符串。

let manPerfumesUTF = String(UTF8String: manPerfumes.cStringUsingEncoding(NSUTF8StringEncoding))

Example in playgrounds: 操场上的例子:

class Products {

    let _name : String

    init(productDict : [String : String]) {

        self._name = productDict["name"] ?? "error"
    }

}

let productDict = ["name" : "Hugo Boss"]
let productDictWithUTF8 = ["name" : "\u{03b2}\u{03b5}\u{03b5}\u{03c1}"]

let hugoBoss = Products(productDict: productDict)
let greekParfume = Products(productDict: productDictWithUTF8)
print(hugoBoss._name)      // Hugo Boss
print(greekParfume._name)  // βεερ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM