从 NSDictionary 获取 NSString 作为 NSArray

Get NSString as NSArray from NSDictionary

我在 NSDictionary 中为具有以下值的对象 field_names 提供了以下数据。

{dBur0z9,nr8r0,R0ru,jrurw,qB5rz9ry},{gr2,Z5uzr,Rwxyr5z0Ar5},^~gr2~cry69v~br9rtyz~Z03r4rsru^~Z5uzr~Uvy3z~dB4srz^5B33,(),9ruI,evD3rsv3,,HLK,evDsBAA65,evDtyvt2s6E,5B33,5B33,5B33,5B33,{6ww},SRiTfUV,5B33,5B33,5B33,5B33

我正在尝试将其获取为 NSArray。我试过了

`NSArray *array = [dictionary objectForKey:@"field_name"];

但是 returns NSString 而不是 NSArray。然后我尝试用 [ ] 替换 { } 并附加 [ ] 使其成为 json 数组,

NSString *s =[[dictionary objectForKey:@"field_names"] stringByReplacingOccurrencesOfString:@"{" withString:@"["];

 s = [s stringByReplacingOccurrencesOfString:@"}" withString:@"]"];

s= [NSString stringWithFormat:@"[%@]",s];

 NSDictionary *temp = @{@"array":s };

 NSLog(@"%@", [temp objectForKey:@"array"]);

但我仍然将其作为 NSString

[[dBur0z9,nr8r0,R0ru,jrurw,qB5rz9ry],[gr2,Z5uzr,Rwxyr5z0Ar5],^~gr2~cry69v~br9rtyz~Z03r4rsru^~Z5uzr~Uvy3z~dB4srz^5B33,(),9ruI,evD3rsv3,,HLK,evDsBAA65,evDtyvt2s6E,5B33,5B33,5B33,5B33,[6ww],SRiTfUV,5B33,5B33,5B33,5B33]

请帮我获取 NSArray!

您将其作为字符串获取,因为它实际上是字符串。我觉得您正在尝试将其解析为 JSON 字符串??

如果是这样,您首先需要该字符串采用有效的 JSON 格式。这是一个例子 JSON

{
   "AnArray": [
      "string 1",
      "string 2",
      "string 3"
   ]
}

(没有换行当然我只是为了可读性添加了它们)

将其作为有效的 JSON 格式化字符串后,您可以执行此操作

NSError *error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData: [dictionary[@"field_names"] dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableContainers error: &error];
NSArray* YourArray = json[@"AnArray"];

我没有找到此任务的任何 cocoa 方法,所以我将 , 替换为 {} 中出现的 &,然后使用 [string componentsSeparatedByString:@","] 方法将其获取为 NSArray。下面是代码

-(NSString *)prepareFields:(NSString *)string {

NSString *remainingString =string;
NSString *newString = @"";
@try {

    while ([remainingString rangeOfString:@"{"].location != NSNotFound ) {

        NSUInteger from = [remainingString rangeOfString:@"{"].location ,
        to =[remainingString rangeOfString:@"}"].location+1 ;

        NSString *part = [[remainingString substringWithRange:NSMakeRange(from,to)] stringByReplacingOccurrencesOfString:@"," withString:@"&"];

        remainingString = [remainingString substringFromIndex:to];

        if([newString isEqualToString:@""])
            newString = [part substringWithRange:NSMakeRange([part rangeOfString:@"{"].location,[part rangeOfString:@"}"].location+1)];

        newString = [NSString stringWithFormat:@"%@,%@",newString,[part substringWithRange:NSMakeRange([part rangeOfString:@"{"].location,[part rangeOfString:@"}"].location+1)]];
    }

}
@catch (NSException *exception) {
    NSLog(@"break %@" ,[exception reason]);
}

if([newString isEqualToString:@""])
    newString = remainingString;
else
    newString = [NSString stringWithFormat:@"%@,%@",newString,remainingString];

return newString;   
}

然后叫它

NSLog([[dictionary objectForKey:@"form"] componentsSeparatedByString:@","]);

给出了以下数组

(
"{dBur0z9&nr8r0&R0ru&jrurw&qB5rz9ry}",
"{dBur0z9&nr8r0&R0ru&jrurw&qB5rz9ry}",
"{gr2&Z5uzr&Rwxyr5z0Ar5}",
"",
"^~gr2~cry69v~br9rtyz~Z03r4rsru^~Z5uzr~Uvy3z~dB4srz^5B33",
"()",
9ruI,
evD3rsv3,
"",
HLK,
evDsBAA65,
evDtyvt2s6E,
5B33,
5B33,
5B33,
5B33,
"{6ww}",
SRiTfUV,
5B33,
5B33,
5B33,
5B33 )