简体   繁体   English

在序列化swift之前编码JSon

[英]Encoding JSon before serialization swift

I have JSON method and work nice on UTF8 webservices, but now have a JSON with another encoding, for example ú = \Ú. 我有JSON方法,并且在UTF8 webservices上运行良好,但现在有一个带有其他编码的JSON,例如ú= \\ u00da。 I know I have to encoding to UTF8 to work my JSON on swift. 我知道我必须编码为UTF8才能在swift上运行我的JSON。 But I don't know how. 但我不知道怎么做。 On request.HTTPBody have a .dataUsingEncoding, it is not enough? 请求.HTTPBody有一个.dataUsingEncoding,这还不够吗?

request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
        let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
            data, response, error in
            if error != nil
            {
                print("error=\(error)")
                return
            }

            do{
                let myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)

                let jsonEmpresas = myJSON["TablaEmp"]!!["item"]
                let empresas: [[String: AnyObject]]

                if let multipleEmpresas = jsonEmpresas as? [[String: AnyObject]] {
                    empresas = multipleEmpresas
                } else if let singleEmpresa = jsonEmpresas as? [String: AnyObject] {
                    empresas = [singleEmpresa]
                } else {
                    empresas = []
                }

                for empresa in empresas{
                    let zcif = empresa["Zcif"] as? String

I had let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) before Do, but was not used 在Do之前我let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) ,但没有使用

ERROR : "Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} " 错误 :“错误域= NSCocoaErrorDomain代码= 3840”字符0周围的值无效。“UserInfo = {NSDebugDescription =字符0周围的值无效}}”

If you want to send emoji or other Unicode character as a parameter You need to encode the HTTP request like postString.dataUsingEncoding(NSUTF8StringEncoding)! 如果要将表情符号或其他Unicode字符作为参数发送您需要对HTTP请求进行编码,如postString.dataUsingEncoding(NSUTF8StringEncoding)! . But as you told on comments this web service will send only Strings or Int So It's okay to use postString.dataUsingEncoding(NSASCIIStringEncoding)! 但是正如你在评论中所说的那样,这个Web服务只会发送字符串或者Int所以可以使用postString.dataUsingEncoding(NSASCIIStringEncoding)! . Because that line affect for only sending parameter, it's encoding sending parameters only not decode the receiving JSON values.So basically just take the JSON value. 因为该行仅影响发送参数,所以它的编码发送参数只是不解码接收的JSON值。所以基本上只取JSON值。

So basically when you return a encoded value apple support to those encode text 所以基本上当你将编码值apple支持返回给那些编码文本时

The data must be in one of the 5 supported encodings listed in the JSON specification: UTF-8, UTF-16LE, UTF-16BE, UTF-32LE, UTF-32BE. 数据必须采用JSON规范中列出的5种支持编码之一:UTF-8,UTF-16LE,UTF-16BE,UTF-32LE,UTF-32BE。 The data may or may not have a BOM. 数据可能有也可能没有BOM。 The most efficient encoding to use for parsing is UTF-8, so if you have a choice in encoding the data passed to this method, use UTF-8 用于解析的最有效编码是UTF-8,因此如果您可以选择对传递给此方法的数据进行编码,请使用UTF-8

https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/JSONObjectWithData:options:error : https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSJSONSerialization_Class/#//apple_ref/occ/clm/NSJSONSerialization/JSONObjectWithData:options:error :

So according to apple and my experience I think Your server DB send the data by using apple unsupported encode option. 所以根据apple和我的经验,我认为你的服务器数据库使用apple unsupported encode选项发送数据。 As I told before, Your Server DB should use UTF-8.Your DB tables using another encoding options. 正如我之前所说,您的服务器数据库应该使用UTF-8.您的数据库表使用其他编码选项。

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

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