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:
Alles anzeigen
Meine Structures:
Alles anzeigen
Mein Code zum Decoden:
Alles anzeigen
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
- {
- "status":"ok",
- "data":[
- {
- "user_id":"4w6fxth",
- "user_username":"MaxMusti",
- "user_profile_picture":"3415330915623892.png",
- "user_questions":[
- {
- "question_title":"Valence ist cool",
- "question_creation":"1530094877",
- "question_expires":"1847998454",
- "question_id":"t4a6adfgGRDa",
- "question_images":[
- "Ohbii3mu.jpg",
- "reec2Bai.jpeg",
- "saeSh6ip.jpg",
- "Taebui9N.jpg"
- ]
- }
- ]
- }
- ],
- "time":1548242690
- }
Meine Structures:
Quellcode
- import Foundation
- struct RootElement: Codable {
- var data = [UserInfo]()
- var status = ""
- var time = Int()
- }
- struct UserInfo: Codable {
- var userID: String
- var userName: String
- var profilePicture: String
- var userQuestions = [QuestionInfo]()
- enum CodingKeys: String, CodingKey {
- case userID = "user_id"
- case userName = "user_username"
- case profilePicture = "user_profile_picture"
- case userQuestions = "user_questions"
- }
- }
- struct QuestionInfo: Codable {
- var questionTitle: String
- var questionCreation: String
- var questionExpires: Int
- var questionID: String
- var questionImages: [String]
- enum CodingKeys: String, CodingKey {
- case questionTitle = "question_title"
- case questionCreation = "question_creation"
- case questionExpires = "question_expires"
- case questionID = "question_id"
- case questionImages = "question_images"
- }
- }
Quellcode
- var items: RootElement = RootElement()
- func setQuestionData() {
- var request = URLRequest(url: baseURL)
- request.setValue(login_session, forHTTPHeaderField: "Authorization")
- request.httpMethod = "POST"
- let task = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
- guard let data = data else { return }
- let decoder = JSONDecoder()
- self.items = (try? decoder.decode(RootElement.self, from: data)) ?? RootElement()
- print(self.items)
- DispatchQueue.main.async {
- self.demoTableView.reloadData()
- }
- }
- task.resume()
- }