hello,
Even if I did research, I found nothing who can help me in my situation.
So, I try to parse Json created by a php script on xcode but I have an error which blocks the process.
I'm new so I triedto do the best for the layout of my question...
My error :
[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 :
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"}]
I hope you will help me, thank you for your answers.
The PHP script is returning JSON, but also a snippet of HTML 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 by using curl from the command line:
curl http://codlobbyz.com/app/service.php
Or by loading it in a browser and viewing the source.
If you have control over the PHP script, remove the analytics code. Otherwise, you could use a regex to remove the non-JSON part of the response before parsing it.
EDIT: To remote the 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With