JSONKit.h中的objectFromJSONString在iOS中返回null

时间:2012-10-22 06:09:55

标签: ios jsonkit

请执行以下操作以重现问题

NSString *url = @"http://qdreams.com/laura/index.php?request=EventWeekListings&year=2012&month=10&day=22";

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
NSString *json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@" , json);
NSDictionary *deserializedData = [json objectFromJSONString];

deserializedData 将包含 nil 。预期的行为是返回正确的字典。

这是因为JSON字典元素的总数超过了某个阈值吗?

我很感激这方面的任何帮助。

2 个答案:

答案 0 :(得分:0)

为什么不使用NSJSONSerialization方法JSONObjectWithData:options:error:它对我来说很好。

    NSString *url = @"http://qdreams.com/laura/index.php?request=EventWeekListings&year=2012&month=10&day=22";
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",arr);

编辑后:我今天早上再次运行代码,就像你一样,我得到了null。 dataWithContentsOfURL的问题。是你无法控制,也无法知道如果出现问题会发生什么。所以,我重新测试了下面的代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [self loadData];
}

-(void) loadData {

    NSLog(@"loadData...");
    self.receivedData = [[NSMutableData alloc] init];
    NSURL *url = [NSURL URLWithString:@"http://qdreams.com/laura/index.php?request=EventWeekListings&year=2012&month=10&day=22"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10.0];
    [NSURLConnection connectionWithRequest:request delegate:self];

}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse...");
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"didReceiveData...");
    NSLog(@"Succeeded! Received %ld bytes of data",[data length]);
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError...");
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    //lblError.text = [NSString stringWithFormat:@"Connection failed! Error - %@",[error localizedDescription]];
    self.receivedData = nil;
}


-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"connectionDidFinishLoading...");
    NSError *error = nil;
    id result = [NSJSONSerialization JSONObjectWithData:self.receivedData options:kNilOptions error:&error];
    if (error) {
        NSLog(@"%@",error.localizedDescription);
        NSLog(@"%@",[[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding]);
    }else{
        NSLog(@"Finished...Download/Parsing successful");
        if ([result isKindOfClass:[NSArray class]]) 
            NSLog(@"%@",result);
    }
}

出现错误,error.localizesDescription的日志是:“无法读取数据,因为它已损坏”。因此,似乎从服务器返回的内容有问题,这会阻止JSON解析器正常工作。我还打印出了字符串以及错误消息。也许你可以仔细看看它,并试图弄清楚数据有什么问题。

答案 1 :(得分:0)

查看你的json你从没有名字的数组值(使用方括号)开始。尝试使用以下内容重新格式化您的响应:

 {"results":[...the rest of your response..]}