2016-12-15 81 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