問題:我無法正確解析JSON文件中的數據到NSArray。 UTF編碼不能按預期工作。Json到iOS編碼錯誤
我的JSON看起來像:
[
{"Name":"Marcos","Address":"1234 Brasil Av. São Paulo - SP","Latitude":"-23.000","Longitude":"-46.70"},{"Name":"Mario","Address":"1000 Washignton Luiz Av. Itú SP","Latitude":"-20.0000","Longitude":"-46.000"}
]
我的Objective-C代碼爲:
NSError *error = nil;
NSURL *jsonUrl = [[NSURL alloc]initWithString:
@"http://marcosdegni.com.br/teste/webservice_teste.php"];
NSString *jsonString = [NSString stringWithContentsOfURL:jsonUrl
encoding:NSUTF8StringEncoding error:&error];
NSLog(@"jsonString: %@ , Error:%@:" ,jsonString, error); //(1)
if (!error) {
NSError *error2 = nil;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSArray * jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error2];
NSLog(@"\n\nArray: %@" \nError:[email protected], jsonArray, error2); //(2)
//(*1*) This log show the content's as they are expected: note the characters ã and ú on the address fields.
//(*2*) The logs from the array and the dictionary show this charters as it's UNIX codes:\U00e and \U00fa respectively.
你爲什麼要解碼相同的數據兩次,一次是數組,一次是字典?什麼不工作? 「錯誤」(你錯誤地提供了'nil')報告(糾正了那個錯誤之後)是什麼? – 2014-11-06 01:57:13
這是我兩次嘗試獲取我的數據。兩人都沒有工作。爲了清晰起見,我排除了這個dic。無 - >錯誤取而代之。 Tks – 2014-11-06 02:04:50
換句話說,沒有什麼不對。擴展的UNICODE數據按照預期顯示在NSLog中。 – 2014-11-06 02:05:31