2016-12-15 85 views
1

请原谅,如果这是一个简单的问题,但我卡住了。我试图阅读我能够自己解决的所有问题。在Swift中获取JSON元素3

我想从JSON数据提取一个URL,我得到的JSON数据正常,我可以将其打印到控制台,但我不能解决如何访问音频文件的URL。

这是我使用来获取JSON代码:

let session = URLSession.shared 
    _ = session.dataTask(with: request, completionHandler: { data, response, error in 
     if let response = response, 
      let data = data, 
      let jsonData = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) { 

      if let dictionary = jsonData as? [String: Any] { 
       if let prounce = dictionary["pronunciations"] as? [String: Any]{ 
        if let audioPath = prounce["audioFile"] as? String { 
         print(audioPath) 
        } 

       } 
      } 

      print(response) 
      print(jsonData) 
     } else { 
      print(error) 
      print(NSString.init(data: data!, encoding: String.Encoding.utf8.rawValue)) 
     } 
    }).resume() 

我得到的输出是:

metadata =  { 
    provider = "Oxford University Press"; 
}; 
results =  (
      { 
     id = maladroit; 
     language = en; 
     lexicalEntries =    (
          { 
       entries =      (
              { 
         etymologies =        (
          "late 17th century: French" 
         ); 
         grammaticalFeatures =        (
                  { 
           text = Positive; 
           type = Degree; 
          } 
         ); 
         senses =        (
                  { 
           definitions =          (
            "inefficient or inept; clumsy:" 
           ); 
           examples =          (
                      { 
             text = "both men are unhappy about the maladroit way the matter has been handled"; 
            } 
           ); 
           id = "m_en_gb0494140.001"; 
          } 
         ); 
        } 
       ); 
       language = en; 
       lexicalCategory = Adjective; 
       pronunciations =      (
              { 
         audioFile = "http://audio.oxforddictionaries.com/en/mp3/maladroit_gb_1.mp3"; 
         dialects =        (
          "British English" 
         ); 
         phoneticNotation = IPA; 
         phoneticSpelling = "\U02ccmal\U0259\U02c8dr\U0254\U026at"; 
        } 
       ); 
       text = maladroit; 
      } 
     ); 
     type = headword; 
     word = maladroit; 
    } 
); 

}

我想要得到的URL在名为audioFile发音。任何帮助深表感谢。

+0

上面的语句显示在使用哪个打印语句? –

+1

显示实际的JSON响应而不是显示控制台输出。 –

回答

0

如果我的猜测是正确的,上面显示的输出在输出顶部没有大括号{

(我也假设输出从您的print(jsonData)获得。)

jsonData是含有两个值的字典:

  • 为“元数据”
  • 阵列的字典值“结果”的值

因此,您无法直接从jsonData(“发音”)中检索“发音”的值或dictionary)。

您可能需要:

  • 检索值从jsonData“成果”,它是一个数组
  • 从“结果”选择一个元素,它是一个字典
  • 检索的价值“lexicalEntries”从结果中,它是一个数组
  • 从“lexicalEntries”中选择一个元素,它是一个字典
  • 从lexicalEntry中检索“发音”的值,i T的数组
  • 从“发音”选择一个元素,它是一个字典

在这里,你可以在每个发音字典访问值。在代码中,你需要做的是这样的:(您可以使用let result = results.first代替!results.isEmpty, case let result = results[0],如果你总是使用第一个元素数组从!...isEmpty, case let...开始以及其他两行。)

if 
    let dictionary = jsonData as? [String: Any], 
    let results = dictionary["results"] as? [[String: Any]], 
    //You need to choose one from "results" 
    !results.isEmpty, case let result = results[0], 
    let lexicalEntries = result["lexicalEntries"] as? [[String: Any]], 
    //You need to choose one from "lexicalEntries" 
    !lexicalEntries.isEmpty, case let lexicalEntry = lexicalEntries[0], 
    let pronunciations = lexicalEntry["pronunciations"] as? [[String: Any]], 
    //You need to choose one from "lexicalEntries" 
    !pronunciations.isEmpty, case let pronunciation = pronunciations[0] 
{ 
    //Here you can use `pronunciation` as a Dictionary containing "audioFile" and some others... 
    if let audioPath = pronunciation["audioFile"] as? String { 
     print(audioPath) 
    } 
} 

您需要逐步从最外层的元素挖掘目标元素。

+0

完美的谢谢你把它打破,现在更有意义,我的代码工作。 – Joby

+0

@Joby,如果有帮助,请不要忘记接受答案 – courteouselk

+0

完成谢谢你的推动。 – Joby