Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Loaner: Codable {
- let loan_amt: String
- let firstname: String
- let combine_sc_details: Details
- struct Details: Codable {
- let dp_sc: Int
- let ltv_sc: Int
- let roi_sc: Float
- let vwBand: String
- //...
- }
- init(from decoder: Decoder) throws {
- let container = try decoder.container(keyedBy: CodingKeys.self)
- self.loan_amt = try container.decode(String.self, forKey: .loan_amt)
- self.firstname = try container.decode(String.self, forKey: .firstname)
- let detailsStrings = try container.decode(String.self, forKey: .combine_sc_details)
- let detailsData = detailsStrings.data(using: .utf8)!
- self.combine_sc_details = try JSONDecoder().decode(Details.self, from: detailsData)
- }
- }
- let jsonStr = """
- [{"loan_amt":"729442","combine_sc_details":"{\\"dp_sc\\": 560000, \\"ltv_sc\\": 315000, \\"roi_sc\\": 15.53, \\"vwBand\\": \\"-\\", \\"vwScore\\": 0, \\"dp_per_sc\\": 64, \\"tenure_sc\\": \\"60\\", \\"veh_offer\\": \\"no offer\\", \\"kuwy_score\\": \\"-1\\", \\"ltv_per_sc\\": 36, \\"loanProduct\\": \\"LP2\\", \\"multi_score\\": \\"-1\\", \\"decision_str\\": \\"SUBJECT TO DETALIED REVIEW\\", \\"product_band\\": \\"-\\", \\"experianScore\\": 0, \\"score_band_str\\": \\"S9\\", \\"subventionType\\": 0, \\"combined_score_int\\": \\"675\\"}","firstname":"HARIPRASANTH"},{"loan_amt":"729442","combine_sc_details":"{\\"dp_sc\\": 715000, \\"ltv_sc\\": 160000, \\"roi_sc\\": 15.53, \\"vwBand\\": \\"-\\", \\"vwScore\\": 0, \\"dp_per_sc\\": 81.71428571428572, \\"tenure_sc\\": \\"60\\", \\"veh_offer\\": \\"no offer\\", \\"kuwy_score\\": \\"-1\\", \\"ltv_per_sc\\": 18.285714285714285, \\"loanProduct\\": \\"LP2\\", \\"multi_score\\": \\"-1\\", \\"decision_str\\": \\"SUBJECT TO DETALIED REVIEW\\", \\"product_band\\": \\"-\\", \\"experianScore\\": 0, \\"score_band_str\\": \\"S9\\", \\"subventionType\\": 0, \\"combined_score_int\\": \\"675\\"}","firstname":"Naveen kumar"},{"loan_amt":"207954","combine_sc_details":"{\\"emi\\": 7308, \\"dp_sc\\": 31046, \\"ltv_sc\\": 249545, \\"roi_sc\\": 17.83, \\"vwBand\\": \\"-\\", \\"vwScore\\": 0, \\"raccRate\\": 17.83, \\"dp_per_sc\\": 12.989958158995815, \\"tenure_sc\\": 48, \\"veh_offer\\": \\"NO OFFER\\", \\"kuwy_score\\": \\"-1\\", \\"ltv_per_sc\\": 100, \\"loanProduct\\": \\"LP2\\", \\"multi_score\\": \\"-1\\", \\"decision_str\\": \\"SUBJECT TO DETALIED REVIEW\\", \\"exshow_price\\": \\"249545\\", \\"product_band\\": \\"-\\", \\"experianScore\\": 0, \\"roiSubvention\\": 0, \\"vehi_discount\\": \\"\\", \\"score_band_str\\": \\"S8\\", \\"sub_ven_Column\\": \\"STANDARD LOAN PRODUCTS\\", \\"subventionType\\": 1, \\"combined_score_int\\": \\"700\\"}","firstname":"KARTHIK"}]
- """
- let jsonData = jsonStr.data(using: .utf8)!
- do {
- let loaners = try JSONDecoder().decode([Loaner].self, from: jsonData)
- print(loaners)
- let sortedByName = loaners.sorted(by: { $0.firstname < $1.firstname })
- sortedByName.forEach { print($0.firstname) }
- let sortedByltv_sc = loaners.sorted(by: { $0.combine_sc_details.ltv_sc < $1.combine_sc_details.ltv_sc })
- sortedByltv_sc.forEach { print($0.firstname) }
- } catch {
- print("Error: \(error)")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement