我正在使用Alamofire提出请求并获取数据。
我的问题在json常量之后开始,当我解析我的词典时,它变为空,不变"结果"变空了。
所以我的代码如下:
URLSession.shared.dataTask(with: request) { (data, response, error) in
do {
guard let data = data else {
throw JSONError.NoData
}
guard let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary else {
throw JSONError.ConversionFailed
}
if let results = json["results"] as? NSDictionary,
let lexicalEntries = results["lexicalEntries"] as? NSDictionary,
let entries = lexicalEntries["entries"] as? NSDictionary,
let senses = entries["senses"] as? NSDictionary {
if let definitions = senses["definitions"] as? String {
print(definitions)
}
}
这是我需要解析的数据模型,常数" json"将其作为字典包含在内。
{
metadata = {
provider = "Oxford University Press";
};
results = (
{
id = hello;
language = en;
lexicalEntries = (
{
entries = (
{
etymologies = (
"early 19th century: variant of earlier hollo; related to holla"
);
homographNumber = 000;
senses = (
{
definitions = (
"used as a greeting or to begin a telephone conversation"
);