gpt4 book ai didi

ios - XCODE 中的 JSON 解析不一致(Cocoa 错误 3840)

转载 作者:行者123 更新时间:2023-11-29 03:42:05 24 4
gpt4 key购买 nike

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

感谢您的帮助!

最佳答案

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

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

关于ios - XCODE 中的 JSON 解析不一致(Cocoa 错误 3840),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com