错误Json:"结束时的NSDebugDescription垃圾"(iOS)

 倒退淂磁带_628 发布于 2023-01-17 11:53

你好,

即使我做了研究,在我的情况下,我找不到任何可以帮助我的人.

所以,我尝试解析在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"}]

我希望你能帮助我,谢谢你的答案.

1 个回答
  • 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);
    }
    

    2023-01-17 11:55 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有