2017-06-19 74 views
0

我在這裏看過很多其他問題,但對於Swift我還是比較新的,我很難將這些答案放到我有的上下文中。在Swift中使用JSON對象填充表格視圖

我有一個事件數據庫,我想用上述事件填充tableview。在我的「EventTableViewController」我有以下功能:

func HTTPRequestListEvent() { 
    let serverURL = NSURL(string: "http://localhost:8888/public_php/Cake/index.php/event/listevent") 
    let request = NSMutableURLRequest(url:serverURL! as URL) 
    request.httpMethod = "POST" 

    //Create a task to send the post request 
    let task = URLSession.shared.dataTask(with: request as URLRequest) { 
     data, response, error in 

     if error != nil { 
      print("HTTPRequestError1=\(error)") 
      return 
     } 

     do { 
      if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject] { 
       print("1") 
       for (_, value) in json { 
        if let eventItem = value as? [String:AnyObject] { 
         if let eventName = eventItem["EventName"] as? String { 
          print(eventName) 
         } 
         if let eventLocation = eventItem["EventLocation"] as? String { 
          print(eventLocation) 
         } 
        } 
       } 
      } 
     } 
     catch { 
      print("HTTPRequestError2=\(error)") 
     } 
    } 
    task.resume() 
} 

不過,我似乎無法得到JSON序列化工作。它不會給我一個錯誤,也不會打印出'1'。

另外我不想說'打印(eventName)',但這是我迄今爲止所有。我想要做的是做這樣的事情:

cell.eventNameLabel.text = eventName 

但我不知道如何獲得在這方面的細胞的細節。

編輯:我的JSON在其他地方工作,但對於單數據 - 我已經能夠登錄和註冊用戶。

有了郵遞員,我從我的服務器的以下JSON數據:

[{ 「事件名稱」: 「華麗晚報é E」, 「EventLocation」: 「熱水浴缸電影」, 「開始日期」:「2017年-12-01「},{」EventName「:」Splendid party「,」EventLocation「:」Gazeebo「,」StartDate「:」2017-12-02「},{」EventName「:」神奇事件「,」 「:」彩虹結束「,」StartDate「:」2017-02-03「},{」EventName「:」稍微垃圾收集「,」EventLocation「:」地下城「,」StartDate「:」2090-01 -04「}]

+1

最有可能您的JSON不是字典。 – rmaddy

+0

你可以添加一個JSON樣本嗎? –

+0

@rmaddy這是什麼意思,在外行的條款和我如何處理這些信息? – Weirdali

回答

0

第一件事第一件事。你的print("1")不起作用的原因很簡單。您正在使用if let打開該值,如果值爲空,則跳過if let內的代碼。在您的代碼中,只需爲if let添加一個else並進行打印,然後打印出來。

現在回到你的問題。您的if let不起作用的原因是您投射JSON字符串的方式。從看你的JSON字符串,你有一個頂層數組。在您的代碼中,您將結果轉換爲[String:AnyObject]這是一個詞典。所以你會得到null,因爲這個投射失敗。要解決您的問題,請使用以下代碼:

let str = "[{\"EventName\":\"Magnificent soirée\",\"EventLocation\":\"Hot tub cinema\",\"StartDate\":\"2017-12-01\"},{\"EventName\":\"Splendid party\",\"EventLocation\":\"Gazeebo\",\"StartDate\":\"2017-12-02\"},{\"EventName\":\"Magical event\",\"EventLocation\":\"The end of a rainbow\",\"StartDate\":\"2017-02-03\"},{\"EventName\":\"Slightly rubbish gathering\",\"EventLocation\":\"Dungeon\",\"StartDate\":\"2090-01-04\"}]" 

do { 
    let data = str.data(using: .utf8) 
    if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [Dictionary<String, Any>] { 
     print("1") 
     for oneItemInJsonArray in json { 
      if let eventName = oneItemInJsonArray["EventName"] as? String { 
       print(eventName) 
      } 
      if let eventLocation = oneItemInJsonArray["EventLocation"] as? String { 
       print(eventLocation) 
      } 
     } 
    }else{ 
     print("fail") 
    } 
} 
catch { 
    print("HTTPRequestError2=\(error)") 
}