【问题标题】:Inconsistent JSON Parsing in XCODE (Cocoa error 3840)XCODE 中的 JSON 解析不一致(Cocoa 错误 3840)
【发布时间】:2013-08-17 00:51:10
【问题描述】:

我使用相同的类和方法通过两个专门编写的 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?

感谢您的帮助!

【问题讨论】:

    标签: ios json html-parsing xcode4.5


    【解决方案1】:

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

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

    【讨论】:

    • 感谢 Rob 的回答和 jsonlint 的链接!
    猜你喜欢
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多