Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

json - Cocoa Error 3840 - NSJSONSerialization

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"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...