[英]Parse json array on iOS
I have the following JSON data: 我有以下JSON数据:
[{"id":"value","first_name":"value","last_name":"value"},
{"id":"value","first_name":"value","last_name":"value"},
{"id":"value","first_name":"","last_name":""}]
Then I implement my code : 然后我实现我的代码:
NSError *err;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://abcd.com/index.php?r=WS/Employee"]];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:url];
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];
if (err)
NSLog(@"JSONObjectWithData error: %@", err);
For which, I get the following error message: 为此,我收到以下错误消息:
JSONObjectWithData error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x9c900f0 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.} JSONObjectWithData错误:错误域= NSCocoaErrorDomain代码= 3840“操作无法完成。(可可错误3840。)”(JSON文本不是以数组或对象开头,并且没有允许设置片段的选项。)UserInfo = 0x9c900f0 {NSDebugDescription = JSON文本不是以数组或对象开头,并且没有允许设置片段的选项。}
Please help me to get those data. 请帮助我获取这些数据。
I think u should put something more in request header. 我认为您应该在请求标头中添加更多内容。 Try convert NSData to NSString and look what is comming for you 尝试将NSData转换为NSString并查看适合您的内容
try this code.... 试试这个代码...。
NSURL * url =[NSURL URLWithString:getDataURL];
NSData * data=[NSData dataWithContentsOfURL:url];
json =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
I fix this by replace request header in my PHP file from 我通过替换来自我的PHP文件中的请求标头来解决此问题
echo "<meta charset=\"utf-8\">";
to 至
ob_start();
header('Content-type: application/json');
NSError *err;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://abcd.com/index.php?r=WS/Employee"]];
NSData *dataFromUrl = [NSData dataWithContentsOfURL:url];
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];
if (err)
NSLog(@"JSONObjectWithData error: %@", err);
Just Change your line 只要改变你的路线
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];
with 与
NSJSONSerialization *jsonData = [NSJSONSerialization JSONObjectWithData:dataFromUrl options: NSJSONReadingMutableContainers error:&err];
NSMutableArray *array = [NSMutableArray arrayWithArray:(NSArray *)jsonData];
NSLog(@"Success:%@", array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.