我在iOS 5上解析下面的JSON字符串很困难.
{"States": [{"Name": "Arizona","Cities": [{"Name": "Phoenix"}]},{"Name": "California","Cities": [{"Name": "Orange County"},{"Name": "Riverside"},{"Name": "San Diego"},{"Name": "San Francisco"}]},{"Name": "Nevada","Cities": [{"Name": "Las Vegas"}]}]}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
- (void) parseJson {
NSError *jsonError = nil;
NSData *jsonData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Locations-JSON" ofType:@"rtf"]];
if (jsonData) {
NSDictionary *jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&jsonError];
if (jsonError) {
NSLog(@"JSON Error: %@", [jsonError localizedDescription]);
return;
}
NSLog(@"%@", jsonObjects);
}
}
Run Code Online (Sandbox Code Playgroud)
我一直收到这个错误:
JSON Error: The operation couldn’t be completed. (Cocoa error 3840.)
我很欣赏这方面的一些帮助,因为我显然无法解决这个问题.
我正在尝试解析从ASP.NET Web服务返回的JSON字符串.返回字符串已简化为:
<anyType d1p1:type="q1:string">[{"Firstname":"Johnny"}]</anyType>
Run Code Online (Sandbox Code Playgroud)
当我在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");
}
Run Code Online (Sandbox Code Playgroud)
我不确定该怎么办?错误似乎在"id result ="行上.
*我认为它可能是我的reutrn字符串的格式,但我在ASP.NET帖子上阅读的所有内容都说这是正确的.
*我已将"NSJSONReadingMutableContainers"更改为"NSJSONReadingMutableLeves"