简体   繁体   English

无法使用Alamofire和SwiftyJSON访问array,JSON响应内部的URL

[英]can not access the url inside of array , JSON response , using Alamofire & SwiftyJSON

Here is the JSON response I got from my URL 这是我从URL获得的JSON响应

 {
 "message" : {
"8" : {
  "post_id" : 141,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477452361.jpg"
    }
  ]
},
"2" : {
  "post_id" : 129,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477280037.jpg"
    }
  ]
},
"9" : {
  "post_id" : 143,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477453054.jpg"
    }
  ]
},
"3" : {
  "post_id" : 131,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477282075.jpg"
    }
  ]
},
"user_posts" : "10",
"4" : {
  "post_id" : 133,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477393524.jpg"
    }
  ]
},
"user_img" : "http:\/\/www.thewoodjoynt.com\/Content\/Images\/Products\/NoImageAvailable.jpg",
"user_id" : null,
"5" : {
  "post_id" : 135,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477393867.jpg"
    }
  ]
},
"user_name" : null,
"6" : {
  "post_id" : 137,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477393932.jpg"
    }
  ]
},
"7" : {
  "post_id" : 139,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1477395902.jpg"
    }
  ]
},
"following" : null,
"followers" : null,
"0" : {
  "post_id" : 110,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1475492476.jpg"
    }
  ]
},
"1" : {
  "post_id" : 112,
  "post_img" : [
    {
      "guid" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/IMG_1475494067.jpg"
    }
     ]
},
"user_type" : false
 }
  }
  keys are &&&&&&&& ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  fatal error: unexpectedly found nil while unwrapping an Optional value

I am using Alamofire 4 & SwiftyJSON to handle the JSON data, here is my function for fetching data and assigning it to a variable : 我正在使用Alamofire 4和SwiftyJSON处理JSON数据,这是我的函数,用于获取数据并将其分配给变量:

func getJSON(){

    let getEndPoint: String = "http://xxx/api/get_user_profile_info/"
    Alamofire.request(getEndPoint)
        .responseJSON { response in

            guard response.result.error == nil else {
                // got an error in getting the data, need to handle it
                print("error calling GET")
                print(response.result.error!)
                return
            }

            if let value = response.result.value {

                let json = JSON(value)
                print(json)

                if let dic = json["message"].dictionary {

                    //for Profile Image
                    if let userUrl = dic["user_img"]?.stringValue {
                        self.user_image = URL(string: userUrl)
                    }

                    //for collectionView Cell Image

                    //For getting only number keys with ascending order
                    let keys = (Array(dic.keys) as [String]).filter { (Int($0) != nil) }.sorted {
                        (s1, s2) -> Bool in return s1.localizedStandardCompare(s2) == .orderedAscending
                    }
                    print("keys are &&&&&&&&",keys)
                    //Loop through the keys Array and append all `post_image`.

                    for key in keys {
                        let post_imgAA = dic[key]?.array
                        for itemsIMG in post_imgAA! {
                            self.post_image = itemsIMG["guid"].URL
                        }
                    }

                    print("user_image are***********************************************")
                    print(self.user_image)

                    print("post_image are***********************************************")
                    print(self.post_image)

                     DispatchQueue.main.async {
                     self.afData = 1
                     self.collectionView.reloadData()
                     }

               }
           }
      }

here I am finding nil value in this loop 在这里我在这个循环中找到零值

for key in keys {
                        let post_imgAA = dic[key]?.array
                        for itemsIMG in post_imgAA! {
                            self.post_image = itemsIMG["guid"].URL
                        }
                    }

Handling JSON data and converting it to foundation object is bit confusing for me, because I am pretty new in this. 处理JSON数据并将其转换为基础对象对我来说有点令人困惑,因为我对此很陌生。 If any kind person could help it would be really helpful. 如果有任何仁慈的人可以帮助,那将是非常有帮助的。 Thank You in advance. 先感谢您。

You are getting nil because key contains dictionary not Array , So change like this. 因为key包含dictionary而不是Array ,所以您得到nil ,所以要这样更改。

for key in keys {
    if let innerDic = dic[key].dictionaryValue, 
       let post_imgAA = innerDic["post_img"].array {
        for itemsIMG in post_imgAA! {
            self.post_image = itemsIMG["guid"].URL
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM