IOS Swift Alamofire JSON Request



我正在尝试使用 Alamofire 发出 JSON 请求,但响应是希腊语,Swift 返回:

Response
{
    id = 4;
    name = "U0395U03bbU03b1U03b9U03ccU03bbU03b1U03b4U03bf"                
    url = "http://www.gaiaskarpos.com/app/images/categories/olive.png";
}

问题出在名称字段。

//Swift Alamofire Request
 Alamofire.request(.GET, "http://gaiaskarpos.com/applegetCategores.php",
                   encoding:.JSON).validate().responseJSON{(response)->
                    Void in print("Response Json : (response.result.value)")

我在阿拉伯语/波斯语字符中遇到了同样的问题,这是我的解决方案:JSON 变量:

{
    message = "U062fU0633U062aU06afU0627U0647 U0645U0639U062aU0628U0631 U0646U06ccU0633U062a";
    status = 0;
}

所以我把 json 投给了[String: AnyObject]

var test = json as? [String : AnyObject] print(test)

它是固定的。你应该试一试。

为什么你的编码 .杰森?您发送 .GET 请求,您无需在请求中指定编码类型。

阿拉莫火文件说:

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
     .responseJSON { response in
         print(response.request)  // original URL request
         print(response.response) // URL response
         print(response.data)     // server data
         print(response.result)   // result of response serialization
         if let JSON = response.result.value {
             print("JSON: (JSON)")
         }
     }

所以使用它。链接 -> http://cocoadocs.org/docsets/Alamofire/1.1.3/和 https://github.com/Alamofire/Alamofire

请求正文中传递参数时,必须指定编码类型。它是为了.开机自检和 .放置请求。

尝试将"名称"打印到调试区域。也许你会得到正常编码的名字......Xcode 有一个缺点:当响应中有某种级别的嵌套数据时,它会在响应中显示非 UTF8 数据,例如 '\U0395\U03bb ... ...\U03b1\U03b9\'。

尝试将 NSUTF8StringEncoding 与 alamofire 响应一起使用

encoding: NSUTF8StringEncoding

尝试使用以下代码序列化 JSON,这可能会对您有所帮助

let dict = NSJSONSerialization.JSONObjectWithData(jsonString.dataUsingEncoding(NSUTF8StringEncoding)!,
                options: nil, error: nil) as [String : String]

最新更新