Вопрос

enter image description hereenter image description hereI am working on JSON data parsing with lots of images downloading and data parsing.I have following code for parsing

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {   

     NSString *responseString = [[NSString alloc] initWithData:webdata encoding:NSASCIIStringEncoding];
     [webdata release];
     [connection release];
     NSDictionary *values = [(NSDictionary*)[responseString JSONValue] objectForKey:@"UserId"];
     NSDictionary *Active = [(NSDictionary*)[responseString JSONValue] objectForKey:@"Active"];
     [responseString release];
     NSString *UserID=[NSString stringWithFormat:@"%@",values];
     NSString *Status=[NSString stringWithFormat:@"%@",Active];
     [WSDelegate WServiceResponseMsg:@"WS_Authenticate_User" withResponse:UserID forParam:Status];
}

I have many classes with above code for parsing but app crashes after some time interval because of SBJSON parser.In instrument it gives app crashed because of low memory warning.enter image description here

Это было полезно?

Решение 3

Finally got solution.Just use below line for JSON parsing.Remove NSMutableDictionary and use id :

 NSError *jsonError = nil;

 id allValues = [NSJSONSerialization JSONObjectWithData:webdata
                                                   options:0
                                                     error:&jsonError];

 NSArray *array = [allValues objectForKey:@"Contestants"];

Другие советы

It is a very wrong assumption that most of the developers have while using SBJSONParser that, it has memory leaks. SBJSONParser does not has any leaks and does not introduces leaks in your code. It is true that INSTRUMENTS tells you that the leak is because of SBJSONParser, but it denotes something else. Leaks are because of the way you have implemented SBJSONParser APIs. You must have done something wrong in your code.

Go to the leaks in your instruments. Open Extended Details toolbar and see the line of code that has leak. Instruments tells you the nearest place where the leak is.

Better option would be to use NSJSONSerialization that come as a part with iOS 5 and above

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
  {  
    NSMutableDictionary *values = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:&error];
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top