Advertisement
Larme

Untitled

Mar 18th, 2021
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 3.17 KB | None | 0 0
  1. struct Loaner: Codable {
  2.     let loan_amt: String
  3.     let firstname: String
  4.     let combine_sc_details: Details
  5.  
  6.     struct Details: Codable {
  7.         let dp_sc: Int
  8.         let ltv_sc: Int
  9.         let roi_sc: Float
  10.         let vwBand: String
  11.         //...
  12.     }
  13.  
  14.     init(from decoder: Decoder) throws {
  15.         let container = try decoder.container(keyedBy: CodingKeys.self)
  16.         self.loan_amt = try container.decode(String.self, forKey: .loan_amt)
  17.         self.firstname = try container.decode(String.self, forKey: .firstname)
  18.         let detailsStrings = try container.decode(String.self, forKey: .combine_sc_details)
  19.         let detailsData = detailsStrings.data(using: .utf8)!
  20.         self.combine_sc_details = try JSONDecoder().decode(Details.self, from: detailsData)
  21.     }
  22.  
  23. }
  24.  
  25. let jsonStr = """
  26. [{"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"}]
  27. """
  28.  
  29. let jsonData = jsonStr.data(using: .utf8)!
  30.  
  31. do {
  32.     let loaners = try JSONDecoder().decode([Loaner].self, from: jsonData)
  33.     print(loaners)
  34.  
  35.     let sortedByName = loaners.sorted(by: { $0.firstname < $1.firstname })
  36.     sortedByName.forEach { print($0.firstname) }
  37.  
  38.     let sortedByltv_sc = loaners.sorted(by: { $0.combine_sc_details.ltv_sc < $1.combine_sc_details.ltv_sc })
  39.     sortedByltv_sc.forEach { print($0.firstname) }
  40. } catch {
  41.     print("Error: \(error)")
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement