Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Swift 3中获取JSON元素_Json_Swift3 - Fatal编程技术网

在Swift 3中获取JSON元素

在Swift 3中获取JSON元素,json,swift3,Json,Swift3,如果这是一个简单的问题,请原谅,但我被卡住了。我试着尽我所能去阅读,自己解决它 我试图从JSON数据中提取URL,我得到了JSON数据,我可以将其打印到控制台,但是我不知道如何访问音频文件的URL 这是我用来获取JSON的代码: let session = URLSession.shared _ = session.dataTask(with: request, completionHandler: { data, response, error in if let r

如果这是一个简单的问题,请原谅,但我被卡住了。我试着尽我所能去阅读,自己解决它

我试图从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;
    }
);
}


我想在发音中获取名为
audioFile
的URL。非常感谢您的帮助

如果我的猜测是正确的,那么上面显示的输出在输出顶部缺少大括号
{

(我还假设输出来自
打印(jsonData)

您的
jsonData
是一个包含两个值的字典:

  • “元数据”的字典值
  • “结果”的数组值
因此,您无法直接从
jsonData
(或
字典
)检索“发音”的值

您可能需要:

  • jsonData
    检索“results”的值,它是一个数组
  • 从“结果”中选择一个元素,它是一个字典
  • 从结果中检索“lexicalEntries”的值,它是一个数组
  • 从“词典”中选择一个元素,它是词典
  • 从lexicalEntry中检索“发音”的值,它是一个数组
  • 从“发音”中选择一个元素,它是字典
在这里,您可以访问每个发音词典中的值。在代码中,您需要执行以下操作:

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)
    }
}
(如果总是将第一个元素用于数组,则可以使用
let result=results.first
而不是
!results.isEmpty,case let result=results[0]
。另外两行从
!…isEmpty,case let…
开始。)


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

上面的语句是在使用哪个print语句时显示的?显示实际的JSON响应,而不是显示控制台输出。非常感谢您将其全部分解,现在更有意义了,而且我的代码正在工作。@Joby,别忘了接受answ呃如果有帮助的话