【发布时间】:2016-07-29 14:57:16
【问题描述】:
我正在尝试使用 Weather Underground API 将天气数据解析到我的应用程序中,使用 Xcode 7.3.1、iOS 9.3 和 JSON(但我在使用其他 API,例如 OpenWeatherMap 时遇到了同样的问题)。
我在构建应用程序时没有收到错误,但是当我在模拟器中调用天气时,我收到“线程 1:信号 SIGABRT”错误。我使用断点推测我的问题来自序列化。
我已经尝试清理我的项目,但我没有双重连接。
当我下载并运行this教程的项目时,我遇到了同样的问题......
这是我的代码:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIButton *affichermeteo;
@property (weak, nonatomic) IBOutlet UILabel *meteo;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)affichermeteo:(id)sender {
NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
[NSURL URLWithString:@"http://api.wunderground.com/api/e5cdee14984e242b/conditions/q/CA/San_Francisco.json"]];
NSError *error;
NSDictionary *allCourses = [NSJSONSerialization
JSONObjectWithData:allCoursesData
options:NSJSONReadingMutableContainers
error:&error];
if( error )
{
NSLog(@"%@", [error localizedDescription]);
}
else {
NSArray *currentobservation = allCourses[@"estimated"];
for ( NSDictionary *theCourse in currentobservation )
{
_meteo.text=theCourse[@"weather"];
}
}
}
@end
我的错误窗口:
提前感谢您的帮助,对不起我的英语,我是法国人!
【问题讨论】:
-
哪条线路崩溃了?您可以编辑您的问题以添加崩溃中的堆栈跟踪吗? (您可能需要在异常上设置断点以判断是哪一行导致崩溃。)
-
崩溃出现在“options:NSJSONReadingMutableContainers”行。
-
这表明服务器正在为您提供格式错误的 JSON。您可以将内容输入不同的 JSON 解析器或 JSON 验证器吗?
-
顺便说一句,您用于从服务器读取 JSON 的代码不适用于生产应用程序。
NSDatainitWithContentsOfURL是同步调用,会导致 UI 冻结,直到数据完全加载。如果出现网络故障,冻结时间可能长达 2 分钟,系统将终止您的应用程序。您需要更改该代码以使用 NSURLSession。 -
是的,在线 JSON 解析器显示 JSON 内容没有任何问题
标签: ios json xcode serialization sigabrt