2013-08-17 56 views
0

我使用相同的類和方法通過兩個專門編寫的API下載數據。其中一個API的作品很好,但另一個會產生錯誤3840XCODE中的JSON解析不一致(可可錯誤3840)

下面是代碼:

+(NSDictionary *)executeSearchRequest:(NSString *)usingThisURL 
{ 
NSError* error = nil; 
usingThisURL = [NSString stringWithFormat:@"%@&format=json&nojsoncallback=1", usingThisURL]; 
NSLog(@"URL Sent to Athletic.net: %@", usingThisURL); 
// usingThisURL = [usingThisURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
// Preceding line makes one call fail and does not correct problem with other. 
NSLog(@"[%@ %@] sent %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), usingThisURL); 
NSData *jsonData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:usingThisURL] encoding:NSUTF8StringEncoding error:nil] dataUsingEncoding:NSUTF8StringEncoding]; 

NSLog(@"jsonData: [%@]",jsonData); 

NSDictionary *results = jsonData ? [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&error] : nil; 
NSLog(@"json NSDictionary results: [%@]", results); 
if (error) NSLog(@"[%@ %@] JSON error: %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), error.localizedDescription); 
NSLog(@"[%@ %@] received %@", NSStringFromClass([self class]), NSStringFromSelector(_cmd), results); 
return results; 
} 

的一個數據看起來是正確解析這樣的,當我把它拉下來使用終端和一個 「捲曲-v」 命令:

{"Search":{"Athlete": 
[{"@IDAthlete":"","@FirstName":"John","@LastName":"Doe","@Gender":"M","@IDSchool":"148","@SchoolName":"All American HS","@City":"Union","@State":"ST"}, 
{"@IDAthlete":"654321`","@FirstName":"Jane","@LastName":"Doe","@Gender":"F","@IDSchool":"18266","@SchoolName":"Any Ol HS","@City":"Union","@State":"ST"},]}} 

不解析看起來像這樣的數據:

{"ROOT":{"@xmlns:sql":"urn:schemas-microsoft-com:xml-sql"/* School Info */,"row": 
{"@SelectedAthlete":"235434"},"HasTrack":{"@AthleteID":"235434"},"Athlete": 
{"@IDAthlete":"235434","@SchoolID":"148","@FirstName":"Jane","@LastName":"Doe","@Gender":"F", 
"SchType":[{"@SortID":"1","@SchoolType":"High School","@DispSchoolTypeAbbrev":"HS","School": 
{"@IDSchool":"148","@RegionID":"1","@SchoolName":"All American","SchoolDivision":[{"@DivisionID":"21036","Season":{"@IDSeason":"2012","@Display":"2012","Grade": 
{"@SingularGradeDesc":"11th Grade","@IDGrade":"11","Distance":[{"@Distance":"2600.00","@Units":"Meters","Result": ... 

我唯一能想到的就是JSON對以xmlns:sql開頭的描述感到窒息,或者說這表明我有一個XML文件而不是JSON格式。 (我之前已經用XML解析過這些數據。)

我可以簡單地操縱數據嗎?還是需要修改生成API?

感謝您的幫助!

回答

1

您的第二個JSON有一個字符串,/* School Info */,與"@xmlns:sql"鍵關聯的值之後。我不知道它是如何得到的(在JSON中間有一個C風格的評論是沒有意義的),但是如果你把它拿出來,你應該是好的。

如果您使用的工具類似http://jsonlint.com,它可以幫助識別這些類型的問題。

+0

謝謝,羅布,爲答案和鏈接到jsonlint! – PhillipOReilly