ios - 在 iOS 上解析 json 数组

标签 ios objective-c json

我有以下 JSON 数据:

[{"id":"value","first_name":"value","last_name":"value"},
 {"id":"value","first_name":"value","last_name":"value"},
 {"id":"value","first_name":"","last_name":""}]

然后我实现我的代码:

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);

为此,我收到以下错误消息:

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.}

请帮我获取这些数据。

最佳答案

我认为你应该在请求 header 中添加更多内容。尝试将 NSData 转换为 NSString,然后看看会发生什么

关于ios - 在 iOS 上解析 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25281241/

相关文章:

java - 对象内的 Jackson Json 列表

javascript - JSON 的数组键值

iOS 调试与发布框架

android - 在 Xamarin Forms 应用程序中包含数千张图像

ios - 将数据从一个 View Controller 传递到另一个给出 null

objective-c - 如何使用 Restkit 在离线一段时间后管理本地更改

ios - 如何将 segue doing pop 设置为 root 并在 Storyboard 中推送一个新的?

jquery - 使用 JSON 和 jQuery 可编辑数据表?

android - 将苹果 Touch ID 或三星指纹识别器与我的应用集成

ios - predicateWithSubstitutionVariables 总是抛出异常