【问题标题】:Having Trouble Parsing JSON解析 JSON 时遇到问题
【发布时间】:2014-03-12 00:11:46
【问题描述】:

所以我创建了一个服务调用,现在它正在工作。这是指向 URL 的链接,您必须看到它才能帮助我:

http://jobs.github.com/positions.json?description=python&location=new+york

由于某种原因,我的代码无法检索数据的字典对象。它返回 SIGABRT。

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *urlString = @"http://jobs.github.com/positions.json?description=python&location=new+york";
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSArray *jobs = json[@" "];

    for (NSDictionary *theJob in jobs)
    {
        NSLog(@"%@", theJob[@"description"]);
    }
}

【问题讨论】:

  • 您从哪里获得 SIGABRT?在哪一行?
  • 旁注:不要在主线程上执行此操作。使用 GCD 的dispatch_async、NSOperationQueue 或 NSURLConnection 的异步方法。
  • 这些方法很大而且有点笨重。我仍然是 Web 服务的初学者,所以我需要先了解基础知识。

标签: ios objective-c json web-services


【解决方案1】:

您的 JSON 是一个字典数组。改变

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *jobs = json[@" "];

NSArray *jobs = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

【讨论】:

  • 我把它留空,因为我不确定要放什么
  • 啊,看来我得等3分钟才能接受你的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-28
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多