在iOS中解析JSON时遇到一些错误?

时间:2013-02-10 02:55:46

标签: ios xcode json nsstring nsarray

这是我的解析代码:

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSError *myError = nil;
NSDictionary *flighttrackjson = [NSJSONSerialization JSONObjectWithData:jsonresponse options:NSJSONReadingMutableLeaves  error:&myError];
NSArray *FlightStatus =  [flighttrackjson objectForKey:@"flightStatuses"];
NSString *FlightID = [FlightStatus valueForKey:@"flightId"];
NSString *DepartACode = [FlightStatus valueForKey:@"departureAirportFsCode"];
NSString *ArivalACode = [FlightStatus valueForKey:@"arrivalAirportFsCode"];
NSArray *DepartATime = [FlightStatus valueForKey:@"departureDate"];
NSArray *AriveATime = [FlightStatus valueForKey:@"arrivalDate"];
NSString *DepartTimeString = [DepartATime valueForKey:@"dateLocal"];
NSString *ArriveTimeString = [AriveATime valueForKey:@"dateUtc"];
NSArray *Delays = [FlightStatus valueForKey:@"delays"];
NSString *DepartDelayMinutes = [Delays valueForKey:@"departureGateDelayMinutes"];
NSString *ArriveDelayMinutes = [Delays valueForKey:@"arrivalGateDelayMinutes"];
NSArray *AirportInfo = [FlightStatus valueForKey:@"airportResources"];
NSString *DepartTerminal = [AirportInfo valueForKey:@"departureTerminal"];
NSString *DepartAGate = [AirportInfo valueForKey:@"departureGate"];
NSString *ArriveTerminal = [AirportInfo valueForKey:@"arrivalTerminal"];
NSString *ArriveAGate = [AirportInfo valueForKey:@"arrivalGate"];
NSString *BaggageClaim = [AirportInfo valueForKey:@"baggage"];
flightID.text=FlightID;
DepartCode.text=DepartACode;
ArrivalCode.text=ArivalACode;
DepartTime.text=DepartTimeString;
ArriveTime.text=ArriveTimeString;
DepartDelay.text=DepartDelayMinutes;
ArriveDelay.text=ArriveDelayMinutes;
DepartTerm.text=DepartTerminal;
DepartGate.text=DepartAGate;
ArriveTerm.text=ArriveTerminal;
ArriveGate.text=ArriveAGate;
Baggage.text=BaggageClaim; 
}

当我跑步时,它给了我这个:

2013-02-09 21:51:40.262位置[7083:c07] - [__ NSArrayI isEqualToString:]:无法识别的选择器发送到实例0x8262b50 2013-02-09 21:51:40.263位置[7083:c07] * 由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [__ NSArrayI isEqualToString:]:无法识别的选择器发送到实例0x8262b50 “

并强调这一点:

返回UIApplicationMain(argc,argv,nil,NSStringFromClass([AppDelegate class]));

我的代码看起来很糟糕,或者有人至少可以解释这个错误可能意味着什么?

以下是我正在使用的内容。 http://pastebin.com/vv8ScBfZ

对不起,我对这一切都很陌生。

感谢您的任何提示!

1 个答案:

答案 0 :(得分:2)

看起来FlightStatus是一个带有一个对象的数组(带有一串键的字典)。因为它是一个数组,所以你必须首先获得那个对象,然后才能在其包含的字典中使用valueForKey :.解决这个问题的最简单方法是改变这个:

NSArray *FlightStatus =  [flighttrackjson objectForKey:@"flightStatuses"];

到此:

NSDictionary *FlightStatus =  [[flighttrackjson objectForKey:@"flightStatuses"] lastObject];