2017-12-18 245 views
0

這裏我試圖解析來自本地服務器的數據,但無法解析它,它返回的是空數據,下面是我的模型類,從中我傳遞給表視圖的數據,任何人都可以幫助我實現它?無法在swift 3中解析Json?

在這裏,我曾附我的圖像下面的JSON格式: enter image description here

代碼:

var homePageModel = [HomeBanner]() 

func HomeBannerDownloadJsonWithURL(){ 
     let url = URL(string: homePageUrl)! 
     let task = URLSession.shared.dataTask(with: url) { (data, response, error) in 
      if error != nil { print(error!); return } 
      do { 
       if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] { 
        for item in jsonObj { 
         print(item) 
         for dict in item { 
          print(dict) 
          let dict = HomeBanner(json: item) 
          self.homePageModel.append(dict!) 
          print(self.homePageModel) 
         } 
        } 
        print(self.homePageModel) 
        DispatchQueue.main.async { 
         self.homeTableView.delegate = self 
         self.homeTableView.dataSource = self 
         self.homeTableView.reloadData() 
        } 
       } 
      } catch { 
       print(error) 
      } 
     } 
     task.resume() 
    } 
    struct HomeBanner { 

    let title : String? 
    let titleInArabic : String? 
    let showTitle : String? 
    var banner = [ChildrenBanners]() 

    init?(json : [String:Any]) { 
     if let customAttribute = json["childran_banners"] as? [[String: AnyObject]] { 
       var result = [ChildrenBanners]() 
       for obj in customAttribute { 
        result.append(ChildrenBanners(json: obj as! [String : String])!) 
       } 
       self.banner = result 
       } else { 
        self.banner = [ChildrenBanners]() 
       } 
     self.title = json["title"] as? String ?? "" 
     print(self.title) 
     self.titleInArabic = json["title_in_arabic"] as? String ?? "" 
     self.showTitle = json["show_title"] as? String ?? "" 
    } 
} 

struct ChildrenBanners { 

    let bannerId : String? 
    let name : String? 
    let status : String? 
    let sliderId : String? 
    let desktopImage : String? 
    let mobileImage : String? 
    let imageAlt : String? 
    let sortOrder : String? 
    let startTime : String? 
    let endTime : String? 

    init?(json : [String:Any]) { 

     self.bannerId = json["banner_id"] as? String ?? "" 
     print(self.bannerId) 
     self.name = json["name"] as? String ?? "" 
     self.status = json["status"] as? String ?? "" 
     self.sliderId = json["slider_id"] as? String ?? "" 
     self.desktopImage = json["desktop_image"] as? String ?? "" 
     self.mobileImage = json["mobile_image"] as? String ?? "" 
     self.imageAlt = json["image_alt"] as? String ?? "" 
     self.sortOrder = json["sort_order"] as? String ?? "" 
     self.startTime = json["start_time"] as? String ?? "" 
     self.endTime = json["end_time"] as? String ?? "" 

    } 

} 
+0

後的實際JSON,而不是圖像的必要性。 – ryantxr

+0

我的Json是本地服務器的網址,如果我發帖也無法訪問@ryantxr – User

+0

你在這個jsonObj中得到了什麼結果? –

回答

0

JSON的根是數組,然後第二級是字典連鍵list1list2等你在你的代碼中缺少這個。應該是這樣的(我沒有編譯它)。

if let data = data, let jsonObj = try JSONSerialization.jsonObject(with: data) as? [[String:[String:Any]]] { 
    for item in jsonObj { 
     for (_, dict) in item { 
      if let obj = HomeBanner(json: dict) { 
       self.homePageModel.append(obj) 
      } 
     } 
    } 

} 

還有很多其他的問題,在你的代碼。像力量解包可選。在範圍內再次使用相同的參數。例如。

for dict in item {        
    let dict = HomeBanner(json: item) 
    // .... 
} 

就像你正在使用dict它隱藏外dict的範圍,則不應該使用相同的名稱PARAM。

0

剛剛嘗試的代碼

  if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [[String:Any]] { 
       self.homePageModel = jsonObj.map{HomeBanner(json: $0)} 


       print(self.homePageModel) 
       DispatchQueue.main.async { 
        self.homeTableView.delegate = self 
        self.homeTableView.dataSource = self 
        self.homeTableView.reloadData() 
       } 
      } 
     } catch { 
      print(error) 
     } 

這些線並沒有使可選初始化爲HomeBannerChildrenBanners只使用init(json : [String : Any]){}兩種結構