使用Swift和Alamofire的JSON结构 [英] JSON structure with Swift and Alamofire

查看:127
本文介绍了使用Swift和Alamofire的JSON结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接着是此问题

我正在尝试引入摘要字段,但它具有进一步的价值.我带来了标题和作者,如下所示:

I am trying to bring in the summary field but it has a further value. I have brought in the title and author as follows:

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONstandard
        // print(readableJSON)

        if let posts = readableJSON["posts"] as? [JSONstandard] {
            for post in posts {
                let title = post["title"] as! String

                let author = post["author"] as! String

                let summary = post["summary"] as! String

                print(author)

                if let imageUrl = post["image"] as? String {
                    let mainImageURL = URL(string: imageUrl )
                    let mainImageData = NSData(contentsOf: mainImageURL!)
                    let mainImage = UIImage(data: mainImageData as! Data)

                    postsinput.append(postinput.init(mainImage: mainImage, name: title, author: author, summary: summary))
                }
            }
            DispatchQueue.main.async {
                self.tableView.reloadData()
            }
        }

    }

    catch {
        print(error)
    }
}

但是,如果我尝试以这种方式进行汇总,则会返回错误.任何帮助将不胜感激.

But if i try the summary that way a error is returned. Any help would be appreciated.

JSON结构

{
    "posts" : [{
        "id": "000000",
        "url": "/content/interview2",
        "date": "2016-11-03 09:01:41",
        "modified": "2016-11-03 09:03:47",
        "title": "An interview",
        "image": "https://www.example.com/sites/default/files/oregood.jpeg",
        "summary": {
            "value": "<p>Latin text here</p>",
            "format": "filtered_html"
        }
    }]
}

推荐答案

在上一个问题中,summary键包含Dictionary,因此您可以尝试这样.

From your previous question response, summary key contains the Dictionary so you can try like this.

guard let dic = post["summary"] as? [String: Any], let summary = dic["value"] as? String else {
    return
}

这篇关于使用Swift和Alamofire的JSON结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆