【问题标题】:how to get string removing parameters [duplicate]如何获取字符串删除参数[重复]
【发布时间】:2013-12-02 05:42:06
【问题描述】:

我正在从 json 字典中获取字符串,但结果字符串在括号中,我必须获取没有后备的字符串

代码是

jsonDictionary  = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];        
NSDictionary *dictResult = [jsonDictionary objectForKey:@"result"];
NSDictionary *dictPronunciations = [dictResult valueForKey:@"pronunciations"];
NSDictionary *dictAudio = [dictPronunciations valueForKey:@"audio"];
NSString *strMp3Path = [dictAudio valueForKey:@"url"];
NSLog(@"str mp3 path %@",strMp3Path);

结果是

(
    (
        "/v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3"
    )
)

我想将/v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3 作为不带括号的字符串。请帮忙...

【问题讨论】:

  • stringByReplacingOccurrencesOfString

标签: ios objective-c cocoa-touch


【解决方案1】:

您正在记录的对象不是 NSString 实例。它是数组中数组内的字符串。

尝试:

NSLog(@"str mp3 path %@",strMp3Path[0][0]);

如果按需要打印,则对象dictAudio 与键url 保持一致,是一个数组,带有一个数组。您应该在将其粘贴到字典中的任何地方修复它。

【讨论】:

    【解决方案2】:

    试试下面的代码:

    NSMutableArray *myArray = [dictAudio valueForKey:@"url"];
    NSString *myStr = [[myArray objectAtIndex:0] objectAtIndex:0];
    NSLog(@"%@", myStr);
    

    【讨论】:

      【解决方案3】:

      使用此代码。如果您的值是 json 中的多个值,则可以不使用大括号将值一一添加:

      NSMutableArray *dictPronunciations = [dictResult valueForKey:@"pronunciations"];
      
      NSMutableArray *arrayPronunciations = [[NSMutableArray alloc] init];
      
      for (int i = 0; i< [dictPronunciations count]; i++)
                       {
                           NSString *string = [dictPronunciations objectAtIndex:i];
      
                           NSLog(@"String = %@",string);
      
                           [arrayPronunciations addObject:string];
                       }
      
      NSLog(@"Array Pronounciations = %@",arrayPronunciations);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-29
        • 1970-01-01
        • 2017-02-15
        • 2015-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多