2017-04-06 20 views
-2

Ich bin neu zu schnell. Ich versuche, in schnellen 3. Daten von URL zu analysieren ich mit diesem Code bin versucht:Alamofire JSON Parsing Swift 3

Alamofire.request("https://jsonplaceholder.typicode.com/todos").responseJSON { response in 

     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 
     } 
    } 

Und ich habe JSON wie folgt:

JSON: (
    { 
    completed = 0; 
    id = 1; 
    title = "delectus aut autem"; 
    userId = 1; 
}, 
    { 
    completed = 0; 
    id = 2; 
    title = "quis ut nam facilis et officia qui"; 
    userId = 1; 
}) 

Wer kann mir helfen, wie alle analysieren von diesem Wert zu Wörterbuch oder Array und es in einer Tabellenansicht in swift 3 präsentieren?

Hier Code für implementieren meine Tableview

override func numberOfSections(in tableView: UITableView) -> Int { 
    // #warning Incomplete implementation, return the number of sections 
    return object.count 
} 
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // #warning Incomplete implementation, return the number of rows 
    return object.property.count 
} 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath) 

    cell.textLabel?.text = property.[indexPath] 

    return cell 
} 
+0

Zeigen Sie Code von 'tableView' –

+0

nur eine einfache Tabellenansicht. die haben 4 Reihe in 1 Abschnitt, um Wert zu zeigen. Danke, wenn du mir hilfst. –

+0

Fügen Sie dann diesen Code in Ihre Frage ein, welche Informationen Sie mit TableCell anzeigen möchten. –

Antwort

0

Modell erstellen wie auf diese Weise

class YourData: NSObject { 

    var completed: Bool 
    var id: String 
    var title: String 
    var userId: String 


    init(dict: NSDictionary) { 
     completed = dict["completed"] as? Bool ?? false 
     id = dict["id"] as? String ?? "" 
     title = dict["title"] as? String ?? "" 
     userId = dict["userId"] as? String ?? "" 
     super.init() 
    } 
} 

diese Parse für Versuchen.

let arrayData : [YourData] = [] 

if let JSON = response.result.value as? [Dictionary] { 
    for dict in JSON { 
     let data = YourData(dict: dict) 
     arrayData.append(data) 
    } 
} 

Jetzt können Sie arrayData verwenden in tableview

+1

In Zeile 3: "für dict in Json", Fehler erhalten: Typ "Any" entspricht nicht Protokoll "Sequenz" –

+0

Antwort aktualisiert Bitte überprüfen Aqnd lassen Sie mich wissen –

+0

** Verwenden Sie nicht 'NSDictionary' in Swift! ** Du wirfst die starke Typinformation weg. – vadian