【发布时间】:2014-11-06 01:50:52
【问题描述】:
问题:我无法将 JSON 文件中的数据正确解析为 NSArray。 UTF 编码未按预期工作。
我的 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:$@, 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.
【问题讨论】:
-
为什么要对相同的数据进行两次解码,一次是数组,一次是字典?什么不起作用?
error(你错误地输入nil)报告了什么(在你纠正了那个错误之后)? -
这是我获取数据的两次尝试。没有一个工作。为了清楚起见,我排除了 dic 。 nil --> 错误被替换。 Tks
-
所以,换句话说,没有错。扩展的 UNICODE 数据按预期显示在 NSLog 中。
-
不,当我记录 jsonArray 时,它仍然显示 \U00e3 而不是 ã 和 \U00fa 而不是 ú...
-
是的!所以是正确的。 (如果您不相信我,请在标签中显示数据。)
标签: ios objective-c json nsjsonserialization