可可错误 3840 - NSJSONSerialization
我正在尝试解析从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.
尝试这个希望它会起作用
Try this hope it will work