2016-12-13 17 views
-1
if statusCode == 200 { 
    let json = response.result.value as? NSDictionary 
    print("JSON FILE") 
    //print(json) 
    let companies = json?["companies"] as? [AnyObject] 
    print(companies) 
    for value in companies! { 
     let address = value["address"] as? String 
     print(address) 
     let schedule = companies?["schedule"] as? [AnyObject]// as? NSDictionary 
     print(schedule) 
      for sch in schedule! { 
     } 
    } 
} 

JSONファイルJSONの解析ここでは、迅速な3

{ 
     "code": "200", 
     "message": "OK", 
     "companies": [ 
     { 
      "id": "1", 
      "img": "doxsun.jpg", 
      "schedule": [ 
      { 
       "id": "1", 
       "company_id": "1", 
       "day": "0", 
       "time_from": "06:00:00", 
       "time_to": "23:00:00" 
      } 
      ] 
     }, 
     { 
      "id": "2", 
      "img": "zalypa.jpg", 
      "schedule": [] 
     } 
     ] 
} 

私はそれを解析する方法を正しく解析するJSONファイルに問題がありますか?私はスケジュールを解析できません。どのようにこれらのすべてのタイプを変換するには?品質を渡す言葉。言葉を渡すにはquality.wordsはquality.wordsを渡すにはquality.wordsはquality.wordsを渡すにはquality.wordsを渡すにはquality.wordsを渡すにはquality.wordsを渡すにはquality.wordsの品質を渡す。

+0

エラーは何ですか?あなたはどのようにリクエストをしますか? AlamofireやAFNetworkingを使用していますか?完全なコードとエラーが表示されますか? – ozgur

+0

alamofireの応答json –

+0

スケジュールに誤りがあります 索引タイプがstringの値タイプanyobjectをサブスクライブできません –

答えて

1

as演算子を使用して正しいタイプに変換に関する問題があります。私は以下のコードを使用して、各企業のスケジュールを反復処理できるようにする必要があります信じて:

if let JSON = response.result.value as? [String: AnyObject] { 
    if let companies = JSON["companies"] as? [[String: AnyObject]] { 
    for company in companies { 
     if let schedules = company["schedule"] as? [[String: AnyObject]] { 
     for schedule in schedules { 
      // do something with the schedule 
     } 
     } 
    } 
    } 
} 
-1
Alamofire.request(url, method: .get, headers: nil).responseJSON{response in 

     switch response.result{ 

     case.success: 


      print("sucess") 

      if let JSON = response.result.value 
      { 

       self.hk = JSON as! NSDictionary 

       print(self.hk) 

       print(((self.hk.value(forKey: "contacts")as! NSArray).object(at: 4  )as! NSDictionary).value(forKey: "name")as! NSString) 

       self.flag = 1 
       self.tbl1.reloadData() 


      } 
     case.failure(let Error): 

      print("error\(Error)") 




     } 



    } 





} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    if flag == 0 
    { 
     return 0 
    } 
    else 
    { 
     return (self.hk.value(forKey: "contacts")as! NSArray).count 

    } 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tbl1.dequeueReusableCell(withIdentifier: "cell", for: indexPath)as! TableViewCell1 

    cell.lbl1.text = (((self.hk.value(forKey: "contacts")as! NSArray).object(at: indexPath.row)as! NSDictionary).value(forKey: "name")as!String) 

    return cell 

}