Question

I download data through two specially written API's using the same class and method. One of the API's works well but the other one produces an error 3840.

Here is the code:

 +(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;
 }

The data for the one looks that parses correctly like this when I pull it down using Terminal and a "curl -v" command:

 {"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"},]}}

The data that does not parse looks like this:

 {"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": ... 

The only thing I can figure is that JSON is choking on the description that begins with xmlns:sql OR that this indicates I have an XML file rather than JSON format. (I have parsed this data in XML before.)

Can I simply manipulate the data as is or do I need to have the generating API modified?

Thanks for your help!

Was it helpful?

Solution

Your second JSON has a string, /* School Info */, after the value associated with the "@xmlns:sql" key. I don't know how that got in there (it makes no sense to have a C-style comment in the middle of a JSON), but if you take that out, you should be good.

If you use a tool like http://jsonlint.com, it can help to identify these sorts of issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top