从NSString获取价值

时间:2017-07-29 07:09:53

标签: ios objective-c json string

从服务器获取复杂的字符串

@"a:3:{i:0;s:4:\"3530\";i:1;s:4:\"3532\";i:2;s:4:\"4503\";}"

我需要上面字符串中的所有ID,如

["3530", "3532", "4503"]

如何获得以上输出?我知道一种方法componentsSeparatedByString将字符串拆分为数组并得到我的结果。

但我认为这不是好方法。那么你能否指出我正确的方向和RGEX或ETC。?

注意:不是JSON字符串,因此我们无法将其转换为ArrayDictionary

我的逻辑得到了预期的结果。但我不喜欢在下面这样做。

更新

 NSString *temStringValue = @"a:3:{i:0;s:4:\"3530\";i:1;s:4:\"3532\";i:2;s:4:\"4503\";}";
    NSArray * arrSplit = [temStringValue componentsSeparatedByString:@"4:\""];
    NSMutableArray *finalArray = [[NSMutableArray alloc] init];
    if(arrSplit.count > 0 ){
        for (NSString *strInner in arrSplit) {
            NSArray * arrSplit2 = [strInner componentsSeparatedByString:@"\""];
            NSLog(@"%@", arrSplit2);
            [finalArray addObject:[arrSplit2 objectAtIndex:0]];
        }
    }

    [finalArray removeObjectAtIndex:0];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalArray options:NSJSONWritingPrettyPrinted error:nil];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", jsonString

输出

[
  "3530",
  "3532",
  "4503"
]

所以有人有好主意吗?

3 个答案:

答案 0 :(得分:0)

尝试此代码

NSString *temStringValue = @"a:3:{i:0;s:4:\"3530\";i:1;s:4:\"3532\";i:2;s:4:\"4503\";}";
    NSMutableArray * arrSplit = [[temStringValue componentsSeparatedByString:@"\""] mutableCopy];
    NSMutableArray *output = [NSMutableArray new];
    [arrSplit removeLastObject];
    [arrSplit enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ( [(NSString *)obj containsString:@"s:4"] == false ) {
            [output addObject:obj];
        }
    }];
    NSLog(@"%@",output);

输出

(
3530,
3532,
4503
)

答案 1 :(得分:0)

由于您要查找的值是"之间唯一的值,因此您可以使用非常简单的正则表达式。

NSString *yourString = @"a:3:{i:0;s:4:\"3530\";i:1;s:4:\"3532\";i:2;s:4:\"4503\";}";

// Find occurrences of integer numbers between double quotes.
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"\"([0-9]+)\"" options:0 error:nil];
NSRange range = NSMakeRange(0, [yourString length]);
NSArray* matches = [regex matchesInString:yourString options:0 range:range];

// Extract the found occurrences as strings.
NSMutableArray* output = [NSMutableArray new];
for (NSTextCheckingResult* match in matches)
{
    [output addObject: [yourString substringWithRange:[match rangeAtIndex:1]]];
}

答案 2 :(得分:0)

这是使用正则表达式的解决方案

模式搜索双引号,后跟一个或多个非双引号字符和第二个双引号。双引号之间的字符在一个组中捕获。

NSString* input = @"a:3:{i:0;s:4:\"3530\";i:1;s:4:\"3532\";i:2;s:4:\"4503\";}";
NSString *pattern = @"\"([^\"]+)\"";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
NSArray *matches = [regex matchesInString:input options:0 range:NSMakeRange(0, input.length)];
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSTextCheckingResult *match in matches) {
    [result addObject: [input substringWithRange:[match rangeAtIndex:1]]];
}
NSLog(@"%@", result);