gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:20:45 27 4
gpt4 key购买 nike

这是我解析 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 浏览器中,两行给出相同的结果:{“结果”:[{“名称”:“Самара,Россия”,“类型”:“城市”,“c”:“RU”,“zmw”:“00000.1.28807”,“tz”: “欧洲/萨马拉”,“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": “欧洲/萨马拉”,“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": "欧洲/罗马", "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 的内容不同?

最佳答案

网址 http://autocomplete.wunderground.com/aq?query=%D0%A1%D0%B0%D0%BC%D0%B0%D1%80%D0%B0还在 Safari 中给出空结果。

这是服务器问题。

关于utf-8 - Swift 中的 NSJSONSerialization 和非拉丁(西里尔)字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24781171/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com