0

我想解析许多 JSON 字符串。这是代码:

while(stations.count > 0) {
        NSString*string = [[[NSString alloc] initWithString:[stations objectAtIndex:0]] retain];
        
        NSMutableDictionary*dic = [[[NSMutableDictionary alloc]init]retain];
        
        NSData*data = [[[NSData alloc] initWithData:[string dataUsingEncoding:NSUTF8StringEncoding]]retain];
        NSMutableDictionary* pars = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]];
        
        [dic setObject:[NSString stringWithString:[pars objectForKey:@"nm"]] forKey:@"nm"];
        [dic setObject:[NSString stringWithString:[pars objectForKey:@"btr"]] forKey:@"btr"];
        [dic setObject:[NSString stringWithString:[pars objectForKey:@"id"]] forKey:@"id"];
        [dic setObject:[NSString stringWithString:[pars objectForKey:@"cntr"]] forKey:@"cntr"];
        [dic setObject:[NSString stringWithString:[pars objectForKey:@"gnr"]] forKey:@"gnr"];
        
        [pars release];
        @try {
            [parsedData addObject:[NSDictionary dictionaryWithDictionary:dic]];
        }
        @catch (NSException* exc) {
            NSLog(@"%@, %@", exc.description, exc.reason);
        }
        [dic release];
        [data release];
        [string release];
        [stations removeObjectAtIndex:0];
        
        if (i%1000==0) {
            NSLog(@"nnnn %i %i", parsedData.count, stations.count);
        }
        
        i++;
        float k = count;
        k = (i + 1)/k;
        
        [self performSelectorOnMainThread:@selector(increaseProgress:) withObject:[NSNumber numberWithFloat:k] waitUntilDone:YES];
    }

在将(通常但不是每次)字符串添加到数组时,我得到错误:

exc_breakpoint(代码=exc_i386_bpt

和:

GuardMalloc [Radiocent new try-1820]:VM 分配 68752 字节失败

GuardMalloc[Radiocent new try-1820]:显式陷入调试器!!!

Stations 数组非常大……大约有 60000 个字符串。

4

1 回答 1

0

首先,当您分配时,您不需要保留,因为保留计数已经 +1。

NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

不是

NSMutableDictionary*dic = [[[NSMutableDictionary alloc]init]retain];

下面的行已经返回可变字典,因此您不必从中创建新字典,如果您想更改,只需调用 - mutableCopy 即可。

NSMutableDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

或创建副本:

NSMutableDictionary *dictionary = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil] mutableCopy];

如果站是 JSON 字符串,则对所有站执行此操作:

NSData *data = [JSONString dataUsingEncoding:NSUTF8StringEncoding];
NSArray *stationsArray = [[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil] mutableCopy];

for (NSDictionary *stationDic in stationArray)
{
     // This will iterate over each station dictionary in the station array
     // Do your stuff here ...
}

对于您的示例:(我已添加 \" 来转义双引号,您不必这样做)

在您的示例周围放置方括号,这使其成为一个数组,您可以使用下面给定的代码遍历每个。(仅在它们不存在时添加)

NSString *example = @"[{\"id\":\"02AB99\",\"btr\":\"24\",\"cntr\":\"461E\",\"gnr\":\"8A51\",\"nm\":\"88.3 Fm Radio Q Jogjakarta\"}, {\"id\":\"026058\",\"btr\":\"160\",\"cntr\":\"461E\",\"gnr\":\"8A7B\",\"nm\":\"88.3 The Dog\"}, {\"id\":\"0300D2\",\"btr\":\"55 64\",\"cntr\":\"461C\",\"gnr\":\"8A75\",\"nm\":\"88.3 The Wind\"}, {\"id\":\"0159E6\",\"btr\":\"128\",\"cntr\":\"4610\",\"gnr\":\"8A6C\",\"nm\":\"88.30 Z Fm Radio Word Online\"}]";

NSError *error = nil;
NSArray *stationArray = [NSJSONSerialization JSONObjectWithData:[example dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:&error];

if (!error)
{
    for (NSDictionary *stationDic in stationArray)
    {
        NSLog(@"\nName: %@ \nDump: \n%@", [stationDic valueForKey:@"nm"], stationDic);
    }
}
于 2013-02-08T12:30:34.053 回答