如何阅读json Dictionary

时间:2013-11-30 05:34:27

标签: ios json

我必须阅读json数据而不是获取.mp3文件我不知道如何获取它请帮助我

我的代码是

 NSArray *ar = [NSArray arrayWithObject:[dict objectForKey:@"pronunciations"]];
 for (NSString *key in [dict objectForKey:@"pronunciations"])
 {
     for (NSDictionary *dictpro in ar)
     {
         NSLog(@"key2   is %@",key);
         NSLog(@"title for key2 is %@",[dict objectForKey:key] );
     }
 }

并且回应是 key2是

{
   audio =    
   (
       {
           type = pronunciation;
           url = "/v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3";
       }
   );

   ipa = "\U0259\U02c8be\U026at";
}

2013-11-30 10:53:37.756 TestJson[419:c07] title for key2 is (null)

我必须阅读.mp3文件,请帮助我。

3 个答案:

答案 0 :(得分:1)

试试这个

//NSDictionary *dict1=[Jsondict objectForKey:@"pronunciations"];
NSArray *array1=[Jsondict objectForKey:@"audio"];
NSDictionary *dict2=[array1 objectatIndex:1];

NSLog(@"%@",[Jsondict objectForKey:@"url"]);
NSLog(@"%@",[Jsondict objectForKey:@"ipa"]);

答案 1 :(得分:1)

知道了, 请执行以下操作

NSDictionary *dictResult = [JSONRepsponse objectForKey:@"result"];
NSDictionary *dictPronunciations = [dictResult valueForKey:@"pronunciations"];
NSDictionary *dictAudio = [dictPronunciations valueForKey:@"audio"];
NSString *strMp3Path = [dictAudio valueForKey:@"url"];
NSString *commonUrl = @"https://api.pearson.com";
NSString *strUrl = [commonUrl stringByAppendingString:strMp3Path];
NSURL *url =  [NSURL URLWithString:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSData *dataMp3 = [NSData dataWithURL : url];

您可以使用此数据在AvAudio播放器中播放Mp3

答案 2 :(得分:-1)

假设这是您的JSON响应(Key2):

{
   audio =    
   (
       {
           type = pronunciation;
           url = "/v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3";
       }
   );

   ipa = "\U0259\U02c8be\U026at";
}

你需要得到"/v2/dictionaries/assets/ldoce/gb_pron/abate0205.mp3" 然后你可以这样做:

NSArray *audio = key2["audio"];

// If there have multiple dictionary in audio array
NSMutableArray *urls = [@[] mutableCopy];
for (NSDictionary *dic in audio) {
    [urls addObject:dic[@"url"]];
}

// Otherwise
NSString *url = audio[0][@"url"];

整体应如下所示:

NSArray *ar = [NSArray arrayWithObject:[dict objectForKey:@"pronunciations"]];
NSMutableArray *urlArray = [@[] mutableCopy];
for (NSString *key in ar) {
    NSArray *audio = key["audio"];
    // If there have multiple dictionary in audio array
    NSMutableArray *urls = [@[] mutableCopy];
    for (NSDictionary *dic in audio) {
        [urls addObject:dic[@"url"]];
    }
    [urlArray addObject:urls];

    // Otherwise
    NSString *url = audio[0][@"url"];
    [urlArray addObject:url];
}