Json Error: "NSDebugDescription Garbage at end" (iOS)

Hello,

Even if I was doing research, I could not find anything to help me in my situation.

So, I am trying to parse the Json generated by the php script on xcode, but I have an error that blocks the process.

I am a beginner, so I tried to do my best for the layout of my question ...

My mistake:

[376:70b] Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Garbage at end.) UserInfo=0x8bc0f70 {NSDebugDescription=Garbage at end. 

My code is:

 NSData *jsonSource = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"http://codlobbyz.com/app/service.php"]]; NSError *err; id jsonObjects = [NSJSONSerialization JSONObjectWithData: jsonSource options:NSJSONReadingMutableContainers error:&err]; NSLog(@"%@", err); 

My json:

 [{"nom":"Call of duty ghost","date":"22 novembre","image":"appicon.png"},{"nom":"Fifa 14","date":"22 novembre","image":"appicon.png"}] 

Hope you can help me, thanks for your answers.

+7
json ios objective-c parsing
source share
2 answers

The PHP script returns JSON, as well as the HTML fragment that follows it:

 [{"nom":"Call of duty ghost","date":"22 novembre","image":"appicon.png"},{"nom":"Fifa 14","date":"22 novembre","image":"appicon.png"}] <!-- Hosting24 Analytics Code --> <script type="text/javascript" src="http://stats.hosting24.com/count.php"></script> <!-- End Of Analytics Code --> 

You can see this using curl from the command line:

 curl http://codlobbyz.com/app/service.php 

Or download it in a browser and browse the source.

If you have control over your PHP script, remove the analytics code. Otherwise, you could use a regex to remove part of the JSON response before parsing it.

EDIT: To remove non-JSON with regex, something like this would work:

 NSString *json = @"[{\"nom\":\"Call of duty ghost\",\"date\":\"22 novembre\",\"image\":\"appicon.png\"},{\"nom\":\"Fifa 14\",\"date\":\"22 novembre\",\"image\":\"appicon.png\"}]\n<!-- Hosting24 Analytics Code -->\n<script type=\"text/javascript\" src=\"http://stats.hosting24.com/count.php\"></script>\n<!-- End Of Analytics Code -->"; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s+<!--.*$" options:NSRegularExpressionDotMatchesLineSeparators error:nil]; NSTextCheckingResult *result = [regex firstMatchInString:json options:0 range:NSMakeRange(0, json.length)]; if(result) { NSRange range = [result rangeAtIndex:0]; json = [json stringByReplacingCharactersInRange:range withString:@""]; NSLog(@"json: %@", json); } 
+12
source share

If you look at the raw data, you will find a “0” inside to remove them, which I used for the loop

 NSData *objectData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; id json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&error]; if (json == nil) { int index = 0; const char *bytes = [data bytes]; for (int i = (int)data.length-1; i > 0; i--) { unsigned char ch = (unsigned char)bytes[i]; NSLog(@"%02hhx", ch); if (ch != 0) { index = i+1; break; } } NSRange dataRange = NSMakeRange(0, index); json = [NSJSONSerialization JSONObjectWithData:[objectData subdataWithRange:dataRange] options:NSJSONReadingMutableContainers error:&error]; } 

This will disconnect all “00” bytes from the end of your data.

0
source share

All Articles