Swift 3 JSON解析 [英] Swift 3 json parsing

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

问题描述

由于Alamofire和SwiftyJSON还不支持Swift 3,我在更新我的应用程序时遇到了麻烦.我有一个网址,该网址会向我返回json,如下所示:

I am running into troubles updating my app as Alamofire and SwiftyJSON are not yet supporting Swift 3. I have a url that would return me json as follows:

{
    "products": [
        {
            "body_html":"",
            "created_at":"2016-03-02T13:56:18+03:00",
            "id":489759251,
            "handle":"product",
            "options":[
                {
                    "id":627488838,
                    "product_id":489759251,
                    "name":"Title",
                    "position":1,
                    "values":[
                        "Default Title"
                    ]
                }
            ],

        },

        {
            "body_html":"",
            "created_at":"2016-03-08T05:17:55+03:00",
            "id":530420915,
            "handle":"product-2",
            "options":[
                {
                    "id":6319359750,
                    "product_id":530420915,
                    "name":"Title",
                    "position":1,
                    "values":[
                        "Default Title"
                    ]
                }
            ],

        },
    ]
}

我需要能够解析该json并列出所有退回的产品,并能够读取每种产品的任何特定属性和子选项.

I need to be able to parse that json and list all returned products and be able to read any specific attribute and sub options of each.

我在这里检查了其他一些问题,找到了几种解决方案,并且能够获取json数据并按照上面的方法进行打印.但是,我无法解析它.

I checked some other questions here and found several solutions and was able to get the json data and printed it as above. But, I couldn't parse it.

let shopUrl = "https://\(apiKey):\(password)@\(hostname)" + "/admin/products.json"

let url = URL(string: shopUrl)
URLSession.shared.dataTask(with:url!, completionHandler: {(data, response, error) in
    if error != nil {
        print(error)
    } else {
        do {
            let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String:Any]
            print(json)
        } catch let error as NSError {
            print(error)
        }
    }
}).resume()

有帮助吗?

推荐答案

要遍历所有产品,您需要提取并将其转换为正确的类型.在这种情况下,[String: Any]的数组.

To loop over all of the products you need to extract and cast it to the correct type. In this case an array of [String: Any].

我提取了相关的代码段并对其进行了一些清理,以使此答案更易读.

I extracted the relevant bit of code and cleaned it up a bit to make this answer more readable.

guard let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any],
    let products = json["products"] as? [[String: Any]]
    else { return }

for product in products {
    guard let id = product["id"] as? Int,
        let options = product["options"] as? [[String: Any]]
        else { return }

    print(id)
    print(options)
}

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

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