可可错误 3840 - NSJSONSerialization

发布于 2025-01-05 18:40:36 字数 986 浏览 0 评论 0原文

我正在尝试解析从 ASP.NET Web 服务返回的 JSON 字符串。返回字符串已简化为这样:

<anyType d1p1:type="q1:string">[{"Firstname":"Johnny"}]</anyType>

当我在 xcode 中运行以下代码时,出现错误“Error Domain=NSCocoaErrorDomainCode=3840”...“JSON 文本未以数组或对象开头,且选项允许片段未设置”

NSURL *url = [NSURL URLWithString:@"http://webserver.com/Service.asmx/GetNames"];
    NSData *data = [NSData dataWithContentsOfURL:url];
      if(data != nil)
        {
            NSError *error = nil;
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                if(error == nil)
        {
            NSLog(@"%@", result);
        }else{
            NSLog(@"%@",error);
        }
    }else{
        NSLog(@"it's nil");
    }

我不太确定在这里要做什么?错误似乎出现在“id result =”行。

*我认为这可能是我的 reutrn 字符串的格式,但我在 ASP.NET 帖子上读到的所有内容都表明这是正确的。

*我已将“NSJSONReadingMutableContainers”更改为“NSJSONReadingMutableLeves”

I'm trying to parse a JSON string returned from a ASP.NET web service. The return string has been simplified to just this:

<anyType d1p1:type="q1:string">[{"Firstname":"Johnny"}]</anyType>

When I run the following code in xcode, I get an error of "Error Domain=NSCocoaErrorDomainCode=3840"..."JSON text did not start with array or object and option to allow fragments are not set"

NSURL *url = [NSURL URLWithString:@"http://webserver.com/Service.asmx/GetNames"];
    NSData *data = [NSData dataWithContentsOfURL:url];
      if(data != nil)
        {
            NSError *error = nil;
            id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
                if(error == nil)
        {
            NSLog(@"%@", result);
        }else{
            NSLog(@"%@",error);
        }
    }else{
        NSLog(@"it's nil");
    }

I'm not really sure what to do here? The error seems to be on the "id result =" line.

*I thought it might be the format of my reutrn string, but everything i read on ASP.NET posts says this is correct.

*I have changed "NSJSONReadingMutableContainers" to be "NSJSONReadingMutableLeves"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2025-01-12 18:40:36

Web 服务是否实际返回该字符串内容中的 部分文档?如果是这样,那就是问题所在:有效的 JSON 字符串很简单:

[{"Firstname":"Johnny"}]

这是您的 Web 响应应该包含的唯一内容。尖括号中的所有内容实际上都不是 JSON 数据,它会导致解析器失效。

Is the Web service actually returning the <anyType d1p1:type="q1:string"> and </anyType> portions of that string within the content of the document? If so, that's the problem: The valid JSON string is simply:

[{"Firstname":"Johnny"}],

This is the only content your Web response should contain. Everything in angled brackets is not actually JSON data, and it's throwing the parser off.

秋日私语 2025-01-12 18:40:36

尝试这个希望它会起作用

if ([operation isKindOfClass:[AFJSONRequestOperation class]] && [operation respondsToSelector:@selector(setJSONReadingOptions:)]) {
    ((AFJSONRequestOperation *)operation).JSONReadingOptions = NSJSONReadingAllowFragments;
}
[httpClient enqueueHTTPRequestOperation:operation];

Try this hope it will work

if ([operation isKindOfClass:[AFJSONRequestOperation class]] && [operation respondsToSelector:@selector(setJSONReadingOptions:)]) {
    ((AFJSONRequestOperation *)operation).JSONReadingOptions = NSJSONReadingAllowFragments;
}
[httpClient enqueueHTTPRequestOperation:operation];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文