2013-02-12 76 views
-1

我曾經能夠解析這個JSON,完全沒有任何錯誤,但突然間它停止工作,它不是我的API密鑰...奇怪的JSON解析錯誤?

這是我想解析的內容: 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

閱讀錯誤消息。看看它在哪裏被提出。 – 2013-02-12 19:01:01

+0

@HotLicks不要指望不可能。 :P – 2013-02-12 19:01:57

回答

1

No, I am not trying to assign the value of array to a string

哦,你是的。

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

FlightStatus是一個數組,而不是一個字典(使用例如JSONlint到漂亮地打印的JSON,你會看到它)。

而且我懷疑有在你的代碼是這樣幾個類似的錯誤,一個特別的在於flightId對應於NSNumber而不是NSString - 你還可以從得到一個崩潰。

請研究JSON的官方參考和NSJSONSerialization類的文檔,以查看哪些JSON數據類型映射到哪些Foundation類(順便提一下,這很合乎邏輯)。

+0

對不起,感謝提示,我一定會讀更多來教育自己。 – 2013-02-12 21:02:41