0

我使用相同的类和方法通过两个专门编写的 API 下载数据。其中一个 API 运行良好,但另一个产生错误 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;
 }

当我使用终端和“curl -v”命令将其拉下时,该数据看起来像这样正确解析:

 {"Search":{"Athlete":
 [{"@IDAthlete":"0123456","@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?

谢谢你的帮助!

4

1 回答 1

1

您的第二个 JSON 在与键/* School Info */关联的值之后有一个字符串。"@xmlns:sql"我不知道它是怎么进来的(在 JSON 中间有一个 C 风格的注释是没有意义的),但如果你把它拿出来,你应该会很好。

如果您使用http://jsonlint.com之类的工具,它可以帮助识别此类问题。

于 2013-08-17T02:57:20.273 回答