Thursday, April 30, 2026
HomeiOS Developmentios - Mix body: Generic Parsing No worth related to key CodingKeys...

ios – Mix body: Generic Parsing No worth related to key CodingKeys utilizing

[ad_1]

I’m making an attempt to parse under josn file, however not in a position to parse (utilizing Mix for service API).

{
  "information": {
    "user_data": {
      "u_id": 14252,
      "first_name": "Amar",
      "last_name": "",
      "e-mail": "pattern@gmail.com",
      "birth_date": "",
      "standing": 1,
      "SubscriptionPlanId": null,
      "subscriptionexpireddate": "2021-11-09T10:40:02.000Z",
      "stripe_customer_id": "",
      "stripe_customer_token": "",
      "picture": "dfdfcf7.jpg",
      "password": "anypassword",
      "nation": "",
      "gender": "",
      "token": "eyJ0eXAiOiJKVI1NiJ9.eyJpc3N1ZWQiOiIyMDIxLTEyLTAyVDEwOjE0OjA1Wjk3In0.MznqecyVK6sozp6Fo3em8Ze_ZqSuE3YR2j_oCb1HsZc",
      "dance_org": "",
      "create_time": "2022-02-10T16:14:56.000Z",
      "facebook_id": "",
      "google_id": "11600922982196",
      "twitter_id": "",
      "apple_id": "",
      "inapp_product_id": "com.amar.buy.key",
      "system": "ios",
      "receipt_data": null,
      "original_transaction_id": "",
      "promo_applied": null,
      "notes": null,
      "pockets": 0,
      "referral": "Ms2dJEqU",
      "group_name": null,
      "model": "iOS 3.1.6 (5)"
    }
  },
  "success": true,
  "current_time": "2021-12-02T10:14:05.953Z",
  "error": 1000
}

Getting under Error

keyNotFound(CodingKeys(stringValue: "user_data", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil)], debugDescription: "No worth related to key CodingKeys(stringValue: "user_data", intValue: nil) ("user_data").", underlyingError: nil))

For testing objective, if I commented information and create_time then working high-quality, however I remark solely information then additionally getting error for current_time.

keyNotFound(CodingKeys(stringValue: "current_time", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No worth related to key CodingKeys(stringValue: "current_time", intValue: nil) ("current_time").", underlyingError: nil))

Right here is my Response mannequin

public struct UserLoginResponse: Decodable {
    enum CodingKeys: String, CodingKey {
        case success, error, information
        case currentTime = "current_time"
    }

    let success: Bool
    let currentTime: String? //": "2021-11-20T13:00:54.654Z",
    let error: Int
    let information: LoggedInUserData

    public init(from decoder: Decoder) throws {
        let container = strive decoder.container(keyedBy: CodingKeys.self)

        information = strive container.decode(LoggedInUserData.self, forKey: .information)
        success = strive container.decode(Bool.self, forKey: .success)
        currentTime = strive container.decode(String.self, forKey: .currentTime)
        error = strive container.decode(Int.self, forKey: .error)
    }
}

public struct LoggedInUserData: Decodable {
    
    enum CodingKeys: String, CodingKey {
        case userData = "user_data"
    }
    
    let userData: LoginedInUserInfo
    
    public init(from decoder: Decoder) throws {
        let container = strive decoder.container(keyedBy: CodingKeys.self)
        userData = strive container.decode(LoginedInUserInfo.self, forKey: .userData)
    }
}

public struct LoginedInUserInfo: Decodable {
    
    enum CodingKeys: String, CodingKey {
        case userID = "u_id"
        case firstName = "first_name"
        case lastName = "last_name"
        case e-mail //= "e-mail"
        case birthDate = "birth_date"
        case standing //= "standing"
        case SubscriptionPlanId //= "SubscriptionPlanId"
        case subscriptionexpireddate //= "subscriptionexpireddate"
        case stripeCustomerId = "stripe_customer_id"
        case stripeCustomerToken = "stripe_customer_token"
        case picture //= "picture"
        case password //= "password"
        case nation //= "nation"
        case gender //= "gender"
        case token //= "token"
        case danceOrg = "dance_org"
        case createTime = "create_time"
        case facebookId = "facebook_id"
        case googleId = "google_id"
        case twitterId = "twitter_id"
        case appleId = "apple_id"
        case inappProductId = "inapp_product_id"
        case system //= "system"
        case receiptData = "receipt_data"
        case originalTransactionId = "original_transaction_id"
        case promoApplied = "promo_applied"
        case notes //= "notes"
        case pockets //= "pockets"
        case referral //= "referral"
        case groupName = "group_name"
        case model //= "model"
    }

    let userID: Int
    var firstName: String
    let lastName: String?
    let e-mail: String?
    let birthDate: String?
    let standing: Int
    let SubscriptionPlanId: String?
    let subscriptionexpireddate: String?
    let stripeCustomerId: String?
    let stripeCustomerToken: String?
    let picture: String?
    let password: String
    let nation: String?
    let gender: String?
    let token: String?
    let danceOrg: String?
    let createTime: String?
    let facebookId: String?
    let googleId: String?
    let twitterId: String?
    let appleId: String?
    let inappProductId: String
    let system: String?
    let receiptData: String?
    let originalTransactionId: String?
    let promoApplied: String?
    let notes: String?
    var pockets: Int
    let referral: String?
    let groupName: String?
    let model: String?
    
    
    
    
    public init(from decoder: Decoder) throws {
            let container = strive decoder.container(keyedBy: CodingKeys.self)

        userID = strive container.decode(Int.self, forKey: .userID)
        firstName = strive container.decode(String.self, forKey: .firstName)
        lastName = strive container.decode(String.self, forKey: .lastName)
        e-mail = strive container.decode(String.self, forKey: .e-mail)
        birthDate = strive container.decode(String.self, forKey: .birthDate)
        standing = strive container.decode(Int.self, forKey: .standing)
        SubscriptionPlanId = strive container.decode(String.self, forKey: .SubscriptionPlanId)
        subscriptionexpireddate = strive container.decode(String.self, forKey: .subscriptionexpireddate)
        stripeCustomerId = strive container.decode(String.self, forKey: .stripeCustomerId)
        stripeCustomerToken = strive container.decode(String.self, forKey: .stripeCustomerToken)
        picture = strive container.decode(String.self, forKey: .picture)
        password = strive container.decode(String.self, forKey: .password)
        nation = strive container.decode(String.self, forKey: .nation)
        gender = strive container.decode(String.self, forKey: .gender)
        token = strive container.decode(String.self, forKey: .token)
        danceOrg = strive container.decode(String.self, forKey: .danceOrg)
        createTime = strive container.decode(String.self, forKey: .createTime)
        facebookId = strive container.decode(String.self, forKey: .facebookId)
        googleId = strive container.decode(String.self, forKey: .googleId)
        twitterId = strive container.decode(String.self, forKey: .twitterId)
        appleId = strive container.decode(String.self, forKey: .appleId)
        inappProductId = strive container.decode(String.self, forKey: .inappProductId)
        system = strive container.decode(String.self, forKey: .system)
        receiptData = strive container.decode(String.self, forKey: .receiptData)
        originalTransactionId = strive container.decode(String.self, forKey: .originalTransactionId)
        promoApplied = strive container.decode(String.self, forKey: .promoApplied)
        notes = strive container.decode(String.self, forKey: .notes)
        pockets = strive container.decode(Int.self, forKey: .pockets)
        referral = strive container.decode(String.self, forKey: .referral)
        groupName = strive container.decode(String.self, forKey: .groupName)
        model = strive container.decode(String.self, forKey: .model)
        }
   
}

Right here is my service supervisor API physique

static func fetch<T: Decodable>(with request: URLRequest, session: URLSessionType) -> AnyPublisher<T, MusicNetworkError>  {
        let decoder = JSONDecoder()
          decoder.keyDecodingStrategy = .convertFromSnakeCase
        return  session.sessionWithConfig().dataTaskPublisher(for: request)
              .tryMap { (information: Information, response: URLResponse) in //tryMap validate present function to validate first, if okay then return information oterwise error. whereas Map doesn't present any validation
                  guard let httpResponse = response as? HTTPURLResponse, httpResponse.statusCode == 200 else {
                      throw URLError(.badServerResponse)
                  }
                  return information
              }
              .decode(kind: T.self, decoder: decoder) //JSONDecoder()
              .mapError({ error in
                swap error {
                case is Swift.DecodingError:
                    print("Error whereas fetch = (error)") **//failing all the time**
                  return .decodingFailed
                case let urlError as URLError:
                  return .sessionFailed(error: urlError)
                default:
                  return .different(error)
                }
              })
              .eraseToAnyPublisher()
    }

Please assist me the place I’m doing unsuitable.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments