如何快速从JSON获取数据

时间:2019-02-08 05:58:22

标签: ios json swift

当格​​式如下时,如何从该网站获取数据...

    {
        "0": {
        "productname":"allen and mikes really cool backpackin book traveling and camping skills fo",
        "imageurl":"http://www0.alibris-static.com/isbn/9781560449126.gif",
        "producturl":"http://www.searchupc.com/rd.aspx?t=ls&p=02072019&u=V971a0eHk9UpUxR1R2tKo3VfOMmYmRVTH50MnxCExT91uqUicf84jXQPpBAx17O7ysaZZ%2fVsMLsOtBwFCO3SeGVrcnFTyzGFcicZUQwhzftY%2bO6n0ruUdEVfmPpxN1w%2fp%2fkBlWS0f06CRIcja9kQ7Ic6OnT5WG72U%2by9%2bojmvoA717KnnlnNY5YHBYK3fqQqW6eOYTnvlYDEyEpV2qNhdw%3d%3d",
        "price":"12.79",
        "currency":"USD",
        "saleprice":"",
        "storename":"Alibris UK: books, movies & music"
        }
    }

let urlString = "http://www.searchupc.com/handlers/upcsearch.ashx?request_type=3&access_token=D5C2984C-80D2-4625-A450-C09FE8880408&upc=\(decodedUrl)"

    if let url = URL(string: urlString) {
        var request = URLRequest(url: url)

        request.httpMethod = "GET"

        let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            if error != nil {
                print(error.debugDescription)
                return
            }

            guard let data = data, let response = response, response is HTTPURLResponse else {
                print("request err")
                return
            }


            if let json = try? JSONSerialization.jsonObject(with: data), let rootDict = json as? [String: Any], let items = rootDict["0"] as? [Any] {
                for item in items {
                    if let dict = item as? [String: Any], let link = dict["imageurl"] as? String, let url = URL(string: link) {
                        print(link)
                        self.downloadImage(from: url)
                    }

                    if let dict = item as? [String: Any], let t = dict["productname"] as? String {
                        self.header.append(t)
                        self.itemTitle.text = t
                        print(self.header[0])

                    }
                }
            }
        }
        task.resume()
    }

1 个答案:

答案 0 :(得分:2)

我认为问题在于您的回复。它应该是server { listen 80; listen [::]:80; server_name app.example.com; root /var/www/app/dist; index index.html; location / { try_files $uri $uri/ /index.html; } } 之类的数组od字典:

[[String: Any]]

在当前情况下,您的 [ { "productname":"allen and mikes really cool backpackin book traveling and camping skills fo", "imageurl":"http://www0.alibris-static.com/isbn/9781560449126.gif", "producturl":"http://www.searchupc.com/rd.aspx?t=ls&p=02072019&u=V971a0eHk9UpUxR1R2tKo3VfOMmYmRVTH50MnxCExT91uqUicf84jXQPpBAx17O7ysaZZ%2fVsMLsOtBwFCO3SeGVrcnFTyzGFcicZUQwhzftY%2bO6n0ruUdEVfmPpxN1w%2fp%2fkBlWS0f06CRIcja9kQ7Ic6OnT5WG72U%2by9%2bojmvoA717KnnlnNY5YHBYK3fqQqW6eOYTnvlYDEyEpV2qNhdw%3d%3d", "price":"12.79", "currency":"USD", "saleprice":"", "storename":"Alibris UK: books, movies & music" } ] 不是数组。这是字典,所以您应该使用:

rootDict["0"]

从服务器端更新JSON响应后,您可以直接使用let items = rootDict["0"] as? [String: Any] 。为:

rootDict