奇怪的JSON解析错误?

时间:2013-02-12 18:45:20

标签: json parsing nsstring nsarray nsurlconnection

我曾经能够完美地解析这个JSON而没有任何错误,但突然间它停止工作,它不是我的API密钥......

这就是我要解析的内容:http://pastebin.com/vv8ScBfZ

这是我的解析代码:

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;

当我运行时,我收到此错误:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0x8aa85f0'

不,我不是试图将数组的值赋给字符串。所以我在这里很困惑。

思想?

谢谢!

1 个答案:

答案 0 :(得分:1)

  

不,我不是试图将数组的值赋给字符串

哦,是的,你做了。

FlightStatus = [flighttrackjson objectForKey:@"flightStatuses"];
FlightID = [FlightStatus valueForKey:@"flightId"];

FlightStatus是一个数组,而不是字典(例如使用JSONlint来打印JSON,你会看到它)。

此外:我怀疑您的代码中存在类似的几个类似错误,其中一个特别是flightId对应NSNumber而不是NSString - 您还会得到一个从那崩溃。

请研究JSON的官方参考和NSJSONSerialization类的文档,以了解哪些JSON数据类型映射到哪些Foundation类(顺便说一下,这是非常合乎逻辑的)。