难以获取JSON字典 [英] having difficulty fetching JSON Dictionary

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

问题描述

我的API回覆是

 items are.....****************** ("user_img",     http://www.xxx/Content/Images/Products/NoImageAvailable.jpg)
  items are.....****************** ("user_posts", 10)
  items are.....****************** ("5", {
  "post_id" : 135,
  "post_img" : [
    {
   "guid" : "http:\/\/www.xxx\/wp-     content\/uploads\/2016\/10\/IMG_1477393867.jpg"
  }
]
})
items are.....****************** ("9", {
"post_id" : 143,
"post_img" : [
  {
    "guid" : "http:\/\/www.xxx\/wp-content\/uploads\/2016\/10\/IMG_1477453054.jpg"
}
]
})
items are.....****************** ("2", {
"post_id" : 129,
"post_img" : [
   {
    "guid" : "http:\/\/www.xxx\/wp-  content\/uploads\/2016\/10\/IMG_1477280037.jpg"
  }
  ]
 })
items are.....****************** ("1", {
 "post_id" : 112,
 "post_img" : [
   {
    "guid" : "http:\/\/www.xxx\/wp-content\/uploads\/2016\/10\/IMG_1475494067.jpg"
}
 ]
})
items are.....****************** ("8", {
 "post_id" : 141,
 "post_img" : [
  {
  "guid" : "http:\/\/www.xxx\/wp-   content\/uploads\/2016\/10\/IMG_1477452361.jpg"
    }
      ]
})

函数从我调用它

 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(jsonM)
                //print("message are***********************************************")
                //print(json["message"].dictionary)
                let message = json["message"].dictionary

                for items in message! {

                    print("items are.....******************", items)


                }


                 //DispatchQueue.main.async {
                // self.afData = 1
               // self.tableView.reloadData()

            }
    }

}

模型类

class ProfileJSON {

    var user_Image: URL?
    var post_image:URL?


    init(items: JSON) {

        self.user_Image = items["user_img"].URL

        let post_imgAA = items["post_img"].array
        for itemsIMG in post_imgAA! {
            self.post_image = itemsIMG["guid"].URL
        }
    }

}

我想让 user_img 显示为个人资料图片,并显示
post_img 以显示图片在 CollectionViewCell 中。发现难以将 JSON 字典转换为 Swift MutableObject 。任何建议,任何教程链接将是伟大的伟大的帮助我。我刚从这个月开始使用 JSON ,发现很困难。

I want to get the user_img to show as profile picture and the post_img for showing picture in the CollectionViewCell . Finding it difficult to convert JSON Dictionary to Swift MutableObject . Any suggestion , any tutorial link would be great great help for me. I have just started to work with JSON from this month , finding it difficult.

推荐答案

p>在 ProfileJSON 中,您需要为 post_image URL 类型的数组c $ c>因为 user_Image 是一次,但$ code> post_image 是多次,然后您可以得到 post_image 从这样的字典。

In your ProfileJSON you need to create array of URL type for post_image because user_Image is once but post_image is coming multiple times and then you can get post_image from dictionary like this.

class ProfileJSON {

    var user_Image: URL?
    var post_image: [URL?] = [URL?]()

    init(dic: [String: Any]) {
        if let userUrl = dic["user_img"] as? String {
            self.user_Image = URL(string: userUrl)
        }

        //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
        }

        //Loop through the keys Array and append all your `post_image`.
        for key in keys {                
            if let innerDic = dic[key] as? [String: Any], 
            let post_imgArray = innerDic["post_img"] as? [[String: Any]] {
                for post in post_imgArray {
                    if let postUrl = post["guid"] as? String {
                        self.post_image.append(URL(string: postUrl))
                    }
                }
            }
        }
    }        
}

在初始化之后,创建 ProfileJSON 的对象

Now create the object of ProfileJSON after initialization of message like this.

if let message = json["message"] as? [String: Any] {
     let profileJSON = ProfileJSON(dic: message)
}

这篇关于难以获取JSON字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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