我对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 ,发现很难。
答案 0 :(得分:2)
在ProfileJSON
中,您需要为URL
创建post_image
类型的数组,因为user_Image
只有post_image
但是post_image
会多次出现,然后您可以从这样的字典中获取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
现在,在message
初始化之后创建if let message = json["message"] as? [String: Any] {
let profileJSON = ProfileJSON(dic: message)
}
的对象。
TaskCompletionSource
答案 1 :(得分:1)
您可以使用DicitonaryObject.objectForKey(“KEYNAME”)从字典中提取详细信息吗?数据类型 。 数据类型将存储在该键中的值。 将其存储在变量中,并在任何地方使用它。