Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
Arrays 如何使用SwiftyJSON和Alamofire创建URL数组?_Arrays_Json_Swift_Alamofire_Swifty Json - Fatal编程技术网

Arrays 如何使用SwiftyJSON和Alamofire创建URL数组?

Arrays 如何使用SwiftyJSON和Alamofire创建URL数组?,arrays,json,swift,alamofire,swifty-json,Arrays,Json,Swift,Alamofire,Swifty Json,我有以下问题,我需要从JSON对象检索URL数组,以便从我的应用程序中的电子商务站点下载产品的所有图片 我得到的JSON如下所示: [ { ........ ........ ......... ........ "images": [ { "id": 976, "date_created": "2016-08-10T15:16:49", "date_modified": "2016-08-10T15:16:49", "src": "https://i2.

我有以下问题,我需要从JSON对象检索URL数组,以便从我的应用程序中的电子商务站点下载产品的所有图片

我得到的JSON如下所示:

  [
{
........
........
.........
........
"images": [
  {
    "id": 976,
    "date_created": "2016-08-10T15:16:49",
    "date_modified": "2016-08-10T15:16:49",
    "src": "https://i2.wp.com/pixan.wpengine.com/wp-content/uploads/2016/07/canasta-familia.jpg?fit=600%2C600&ssl=1",
    "name": "canasta-familia",
    "alt": "",
    "position": 0
  }
], 
.......
.......
.......
到目前为止,我只能从数组中获取一个字符串

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
        .responseJSON { response in
 if let jsonValue = response.result.value {
                let jsonObject = JSON(jsonValue)
                var jsonArray = jsonObject[0]["images"][0]["src"].stringValue
                print(jsonArray)
    }
}
这给了我这个

https://xx.xx.xx/xxxx.xxxxx.xxxx/xx-xxxxx/uploads/2016/07/canasta-familia.jpg?fit=600%2C600&ssl=1
但我需要的是访问“images”和“src”中的所有元素,而不仅仅是这两者索引的第一个元素

我该怎么做


有什么想法吗?

步骤1

创建一个自定义对象来表示图片。我们称之为“图片”

第二步:

创建一个数组以保存所有产品图片。确保您注意创建它的范围。理想情况下,它应该在您的下载功能之外

var productPictures = [Picture]()
第三步:

下载JSON文件,为每个图像创建一个图片结构,并将每个图片添加到productPictures数组中

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:

                self.productPictures.removeAll()

                guard let json = response.result.value as? [String:Any] else {
                    print("couldn't retrieve json as a dictionary")
                    return
                }

                guard let images = json["images"] as? [AnyObject] else {
                    print("there was a problem accessing the images key")
                    return
                }

                for image in images {

                    guard let id = image["id"] as? Int,
                          let date_created = image["date_created"] as? String,
                          let date_modified = image["date_modified"] as? String,
                          let src = image["src"] as? String,
                          let name = image["name"] as? String,
                          let alt = image["alt"] as? String,
                          let position = image["position"] as? Int
                    else {
                        print("There was a problem accessing one of the picture variables, or it was missing")
                        continue
                    }

                    let newPicture = Picture(id: id,
                                             date_created: date_created,
                                             date_modified: date_modified,
                                             src: src,
                                             name: name,
                                             alt: alt,
                                             position: position)

                    self.productPictures.append(newPicture)
                }
            case .failure(let error):
                print("could not download and retrieve product images. An error occurred: \(error)")
                return
            }
    }
现在您有了一个充满图片结构的数组,每个都包含从JSON下载中提取的所有必要信息

注意


这不使用SwiftyJSON,但它应该可以工作,并提供相同的预期结果。我希望这有帮助

步骤1

创建一个自定义对象来表示图片。我们称之为“图片”

第二步:

创建一个数组以保存所有产品图片。确保您注意创建它的范围。理想情况下,它应该在您的下载功能之外

var productPictures = [Picture]()
第三步:

下载JSON文件,为每个图像创建一个图片结构,并将每个图片添加到productPictures数组中

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:

                self.productPictures.removeAll()

                guard let json = response.result.value as? [String:Any] else {
                    print("couldn't retrieve json as a dictionary")
                    return
                }

                guard let images = json["images"] as? [AnyObject] else {
                    print("there was a problem accessing the images key")
                    return
                }

                for image in images {

                    guard let id = image["id"] as? Int,
                          let date_created = image["date_created"] as? String,
                          let date_modified = image["date_modified"] as? String,
                          let src = image["src"] as? String,
                          let name = image["name"] as? String,
                          let alt = image["alt"] as? String,
                          let position = image["position"] as? Int
                    else {
                        print("There was a problem accessing one of the picture variables, or it was missing")
                        continue
                    }

                    let newPicture = Picture(id: id,
                                             date_created: date_created,
                                             date_modified: date_modified,
                                             src: src,
                                             name: name,
                                             alt: alt,
                                             position: position)

                    self.productPictures.append(newPicture)
                }
            case .failure(let error):
                print("could not download and retrieve product images. An error occurred: \(error)")
                return
            }
    }
现在您有了一个充满图片结构的数组,每个都包含从JSON下载中提取的所有必要信息

注意


这不使用SwiftyJSON,但它应该可以工作,并提供相同的预期结果。我希望这有帮助

以下代码行应该可以工作,因为我已经用实际的数据集对自己进行了测试

import Alamofire
import SwiftyJSON

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
    .responseJSON { response in
    if let jsonValue = response.result.value {
        let jsonObject = JSON(jsonValue)
        if let array = jsonObject.array {
            for i in 0..<array.count {
                if let images = array[i]["images"].array {
                    for i in 0..<images.count {
                        let src = images[i]["src"]
                        print(src) // save src in an array or whatever
                    }
                }
            }
        }       
    }
导入Alamofire
导入快捷JSON
请求(url,方法:.get,参数:nil,编码:URLEncoding.default,头:头)
.responseJSON{中的响应
如果让jsonValue=response.result.value{
让jsonObject=JSON(jsonValue)
如果let array=jsonObject.array{

对于0中的i..以下代码行应该可以工作,因为我已经用实际的数据集测试了自己

import Alamofire
import SwiftyJSON

Alamofire.request(url, method: .get, parameters: nil, encoding: URLEncoding.default, headers: headers)
    .responseJSON { response in
    if let jsonValue = response.result.value {
        let jsonObject = JSON(jsonValue)
        if let array = jsonObject.array {
            for i in 0..<array.count {
                if let images = array[i]["images"].array {
                    for i in 0..<images.count {
                        let src = images[i]["src"]
                        print(src) // save src in an array or whatever
                    }
                }
            }
        }       
    }
导入Alamofire
导入快捷JSON
请求(url,方法:.get,参数:nil,编码:URLEncoding.default,头:头)
.responseJSON{中的响应
如果让jsonValue=response.result.value{
让jsonObject=JSON(jsonValue)
如果let array=jsonObject.array{


对于0中的我..你能让你的整个JSON数据集在某个地方可下载吗?你能让你的整个JSON数据集在某个地方可下载吗?这看起来是一个很棒的解决方案,我明天将尝试它。非常感谢!不客气!我希望它有帮助。出于某种原因,我收到了此错误消息。(“无法将JSON检索为字典”)嗯,这很奇怪。你的json结构像字典吗?也许它是一个数组,里面有一个字典?这些都很容易修复。这是一个字典数组,我尝试了@el tomato的方法,效果很好,但最好知道在这样的情况下该怎么做。这看起来是一个很棒的解决方案。我明天会尝试的。非常感谢!你是welcome!我希望它能有所帮助。由于某些原因,我收到了此错误消息。(“无法将json作为字典检索”)嗯,这很奇怪。你的json结构是一个字典吗?也许是一个数组,里面有一个字典?这些都很容易修复。这是一个字典数组,我尝试了@el tomato的方法,它起了作用,但最好知道在这样的情况下该怎么做。我得到了以下问题,键入'Bool'不符合协议'Sequence'this发生在指令中循环的开头:对于0中的i