Swift中的NSJSONSerialization和非拉丁(西里尔)字符

时间:2014-07-16 12:45:56

标签: utf-8 swift ios8 nsjsonserialization

这是我解析JSON的函数:

func jsonParsingWeather(urlPath:String) -> NSDictionary {
    var utf8URLPath = urlPath.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
    println("URL path for JSON: \(utf8URLPath)")
    var streamData:NSData = NSData(contentsOfURL: NSURL(string: utf8URLPath))
    var error: NSError?
    var fullWeatherDictionary: NSDictionary = NSJSONSerialization.JSONObjectWithData(streamData, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSDictionary
    println("Full parsing array: \(fullWeatherDictionary)")
    return fullWeatherDictionary
}

发现西里尔字符处理存在问题

urlPath = http://autocomplete.wunderground.com/aq?query=Самара

utf8URLPath = http://autocomplete.wunderground.com/aq?query=%D0%A1%D0%B0%D0%BC%D0%B0%D1%80%D0%B0

在Safari浏览器中,两行都给出相同的结果: { "RESULTS": [ { "name": "Самара, Россия", "type": "city", "c": "RU", "zmw": "00000.1.28807", "tz": "Europe/Samara", "tzs": "SAMT", "l": "/q/zmw:00000.1.28807", "ll": "53.250000 50.450001", "lat": "53.250000", "lon": "50.450001" } ] }

但是!数组fullWeatherDictionary为空!

{
    RESULTS =     (
    );
}

为了比较,请使用拉丁字符串:

urlPath = http://autocomplete.wunderground.com/aq?query=Samara

utf8URLPath = http://autocomplete.wunderground.com/aq?query=Samara

在Safari浏览器中,两行都给出相同的结果: { "RESULTS": [ { "name": "Samara, Russia", "type": "city", "c": "RU", "zmw": "00000.1.28807", "tz": "Europe/Samara", "tzs": "SAMT", "l": "/q/zmw:00000.1.28807", "ll": "53.250000 50.450001", "lat": "53.250000", "lon": "50.450001" }, { "name": "Samarate, Italy", "type": "city", "c": "IT", "zmw": "00000.101.16066", "tz": "Europe/Rome", "tzs": "CEST", "l": "/q/zmw:00000.101.16066", "ll": "45.625370 8.783435", "lat": "45.625370", "lon": "8.783435" } ...........

因此,数组fullWeatherDictionary是:

{
    RESULTS =     (
                {
            c = RU;
            l = "/q/zmw:00000.1.28807";
            lat = "53.250000";
            ll = "53.250000 50.450001";
            lon = "50.450001";
            name = "Samara, Russia";
            type = city;
            tz = "Europe/Samara";
            tzs = SAMT;
            zmw = "00000.1.28807";
        },
.......................

有什么问题?为什么数组fullWeatherDictionary的内容不同,在非拉丁字符的情况下?

2 个答案:

答案 0 :(得分:0)

网址http://autocomplete.wunderground.com/aq?query=%D0%A1%D0%B0%D0%BC%D0%B0%D1%80%D0%B0也会在Safari中显示空结果。

这是一个服务器问题。

答案 1 :(得分:0)

该死!对不起,问题解决了:

我在俄语和地区配置了iOS模拟器,一切都很好 - 数组中的值以任何语言显示