-1

我曾经能够完美地解析这个 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'

不,我不是想将数组的值分配给字符串。所以我在这里很困惑。

想法?

谢谢!

4

1 回答 1

1

不,我不是想将数组的值分配给字符串

哦,是的。

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

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

此外:我怀疑您的代码中有几个类似的错误,一个特定的错误flightId对应于一个NSNumber而不是一个NSString- 你也会因此而崩溃。

请研究 JSON 的官方参考和NSJSONSerialization类的文档,以查看哪些 JSON 数据类型映射到哪些 Foundation 类(顺便说一句,这很合乎逻辑)。

于 2013-02-12T18:58:39.803 回答