你如何将这个NSString分成NSDictionary?

时间:2013-05-19 22:04:30

标签: objective-c arrays nsmutablearray nsmutabledictionary

我从一些linux盒子里获得了一些数据,并希望将它放入NSDictionary中以便以后处理。

你如何将这个NSString变成NSDictionary,如下所示?

data (
eth0 (Errors => 0, Bytes => 32, Packtes => 0, Index => 2,...)
eth1 (Errors => 0, Bytes => 32, Packtes => 0, Index => 2,...)
lo (Errors => 0, Bytes => 32, Packtes => 0, Index => 2,...)
...)

每个值都应该是可访问的,如:

NSString eth0_errors = [[data objectForKey:@"eth0"] objectForKey:@"Errors"];

数据字符串如下所示:

NSString *dummydata = [NSString stringWithFormat:@"+ 1\n"
                       "eth0 Errors 0\n"
                       "eth0 Bytes 34\n"
                       "eth0 Packets 0\n"
                       "eth0 Index 2\n"
                       "eth0 Type ETHER\n"
                       "eth0 MAC 00:0c:29:39:81:9c\n"
                       "eth0 MTU 1500\n"
                       "eth0 Broadcast ff:ff:ff:ff:ff:ff\n"
                       "eth0 Base-Addr 00000000\n"
                       "eth0 IRQ 0\n"
                       "eth0 Realm intern\n"
                       "eth0 Flags UP BROADCAST\n"
                       "eth0 Features HW-CSUM HW-VLAN-TX HW-VLAN-RX HW-VLAN-FILTER\n"
                       "eth0 Speed 1000Mb/s\n"
                       "eth0 Duplex Full\n"
                       "eth0 Negotiation on\n"
                       "eth0 Link up\n"
                       "eth0 Switch\n"
                       "eth1 Errors 0\n"
                       "eth1 Bytes 0\n"
                       "eth1 Packets 0\n"
                       "eth1 Index 3\n"
                       "eth1 Type ETHER\n"
                       "eth1 MAC 00:0c:29:39:81:a6\n"
                       "eth1 MTU 1500\n"
                       "eth1 Broadcast ff:ff:ff:ff:ff:ff\n"
                       "eth1 Base-Addr 00000000\n"
                       "eth1 IRQ 0\n"
                       "eth1 Realm unknown\n"
                       "eth1 Flags BROADCAST\n"
                       "eth1 Features SG/IO HW-CSUM HW-VLAN-TX HW-VLAN-RX HW-VLAN-FILTER\n"
                       "eth1 Negotiation on\n"
                       "eth1 Link DOWN\n"
                       "eth1 Switch\n"
                       "eth1 Speed ?\n"
                       "eth1 Duplex ?\n"
                       "lo Errors 0\n"
                       "lo Bytes 79\n"
                       "lo Packets 1\n"
                       "lo Index 1\n"
                       "lo Type LOOPBACK\n"
                       "lo MAC 00:00:00:00:00:00\n"
                       "lo MTU 3500\n"
                       "lo Broadcast 00:00:00:00:00:00\n"
                       "lo Base-Addr 00000000\n"
                       "lo IRQ 0\n"
                       "lo Realm opsys\n"
                       "lo Flags UP LOOPBACK\n"
                       "lo Features SG/IO NO-CSUM HIGH-DMA FRAGLIST\n"
                       "lo Link up\n"
                       "lo Switch\n"
                       "lo Speed ?\n"
                       "lo Duplex ?\n"
                       "lo Negotiation ?\n"
                       ".\n"];

1 个答案:

答案 0 :(得分:2)

假设您的数据始终符合您指定的格式,您可以使用以下内容:

NSArray *components = [dummydata componentsSeparatedByString:@"\n"];
NSMutableDictionary *dictionary = [NSMutableDictionary new];

for(NSString *component in components) {
    NSArray *subcomponents = [component componentsSeparatedByString:@" "];

    if(subcomponents.count >= 3) {
        NSMutableDictionary *subdictionary = [dictionary objectForKey:[subcomponents objectAtIndex:0]];

        if(subdictionary == nil) {
            subdictionary = [NSMutableDictionary new];
            [dictionary setObject:subdictionary forKey:[subcomponents objectAtIndex:0]];
        }

        if(subcomponents.count > 3) {
            [subdictionary setObject:[subcomponents subarrayWithRange:NSMakeRange(2, subcomponents.count - 2)] forKey:[subcomponents objectAtIndex:1]];
        } else {
            [subdictionary setObject:[subcomponents objectAtIndex:2] forKey:[subcomponents objectAtIndex:1]];
        }
    }
}

请注意,数值将表示为NSStrings。如果您希望它们是NSNumbers,则需要在循环中执行其他检查和转换。