2016-11-07 70 views
0

我從API響應有困難的是,我把它叫做獲取JSON字典

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顯示爲prof ile picture和 post_img用於在CollectionViewCell中顯示圖片。難以轉換JSON字典SwiftMutableObject。任何建議,任何教程鏈接對我來說都是非常大的幫助。我剛從本月開始與JSON一起工作,發現它很困難。

enter image description here

回答

2

在你ProfileJSON需要爲post_image創建URL類型的數組,因爲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)) 
        } 
       } 
      } 
     } 
    }   
} 

現在的message這樣初始化後創建的ProfileJSON的對象。

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

我需要一段時間才能消化哈哈,但非常感謝您的幫助。你是一個傳奇。 –

+0

@ArafinMacRussell歡迎隊友:) –

1

可以使用DicitonaryObject.objectForKey( 「KEYNAME」)作爲提取字典詳細資料?數據類型 。 數據類型將存儲在該密鑰中的值。 將它存儲在一個變量中,並隨時隨地使用它。