你好,
即使我做了研究,在我的情况下,我找不到任何可以帮助我的人.
所以,我尝试解析在xcode上由php脚本创建的Json,但是我有一个阻止进程的错误.
我是新人,所以我努力为我的问题布局做最好的...
我的错误:
[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.
我的代码:
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);
我的json:
[{"nom":"Call of duty ghost","date":"22 novembre","image":"appicon.png"},{"nom":"Fifa 14","date":"22 novembre","image":"appicon.png"}]
我希望你能帮助我,谢谢你的答案.
PHP脚本返回JSON,但也是一个跟随它的HTML片段:
[{"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 -->
您可以通过命令行使用curl来查看:
curl http://codlobbyz.com/app/service.php
或者通过在浏览器中加载并查看源代码.
如果您可以控制PHP脚本,请删除分析代码.否则,您可以使用正则表达式在解析之前删除响应的非JSON部分.
编辑:要使用正则表达式远程非JSON,这样的东西可以工作:
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); }