JSON Decoding Problem

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • JSON Decoding Problem

    Hallo,

    wie meiner Überschrift zu entnehmen habe ich aktuell ein Problem damit ein JSON Paket zu Decoden was ich mir jedoch nicht ganz erklären kann. Vielleicht habe ich einen Denkfehler oder bin einfach blind aber ich sehe nicht warum das ganze nicht funktionieren will...

    Vielleicht kann mir einer von euch helfen und sieht etwas was ich nicht sehe :/

    Das JSON Paket:

    Quellcode

    1. {
    2. "status":"ok",
    3. "data":[
    4. {
    5. "user_id":"4w6fxth",
    6. "user_username":"MaxMusti",
    7. "user_profile_picture":"3415330915623892.png",
    8. "user_questions":[
    9. {
    10. "question_title":"Valence ist cool",
    11. "question_creation":"1530094877",
    12. "question_expires":"1847998454",
    13. "question_id":"t4a6adfgGRDa",
    14. "question_images":[
    15. "Ohbii3mu.jpg",
    16. "reec2Bai.jpeg",
    17. "saeSh6ip.jpg",
    18. "Taebui9N.jpg"
    19. ]
    20. }
    21. ]
    22. }
    23. ],
    24. "time":1548242690
    25. }
    Alles anzeigen


    Meine Structures:

    Quellcode

    1. import Foundation
    2. struct RootElement: Codable {
    3. var data = [UserInfo]()
    4. var status = ""
    5. var time = Int()
    6. }
    7. struct UserInfo: Codable {
    8. var userID: String
    9. var userName: String
    10. var profilePicture: String
    11. var userQuestions = [QuestionInfo]()
    12. enum CodingKeys: String, CodingKey {
    13. case userID = "user_id"
    14. case userName = "user_username"
    15. case profilePicture = "user_profile_picture"
    16. case userQuestions = "user_questions"
    17. }
    18. }
    19. struct QuestionInfo: Codable {
    20. var questionTitle: String
    21. var questionCreation: String
    22. var questionExpires: Int
    23. var questionID: String
    24. var questionImages: [String]
    25. enum CodingKeys: String, CodingKey {
    26. case questionTitle = "question_title"
    27. case questionCreation = "question_creation"
    28. case questionExpires = "question_expires"
    29. case questionID = "question_id"
    30. case questionImages = "question_images"
    31. }
    32. }
    Alles anzeigen
    Mein Code zum Decoden:

    Quellcode

    1. var items: RootElement = RootElement()
    2. func setQuestionData() {
    3. var request = URLRequest(url: baseURL)
    4. request.setValue(login_session, forHTTPHeaderField: "Authorization")
    5. request.httpMethod = "POST"
    6. let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
    7. guard let data = data else { return }
    8. let decoder = JSONDecoder()
    9. self.items = (try? decoder.decode(RootElement.self, from: data)) ?? RootElement()
    10. print(self.items)
    11. DispatchQueue.main.async {
    12. self.demoTableView.reloadData()
    13. }
    14. }
    15. task.resume()
    16. }
    Alles anzeigen
  • Quellcode

    1. question_expires
    ist ein String, kein Int ;) Änder es mal zu String und es wird klappen.

    Mit

    Quellcode

    1. try! decoder.decode(RootElement.self, from: data)
    hättest du auch eine Fehlermeldung bekommen, alternativ natürlich



    C-Quellcode

    1. do {
    2. self.items = try decoder.decode(RootElement.self, from: data)
    3. }
    4. catch {
    5. print(error)
    6. }
    zumindest mal für die Fehlersuche.