Map
we need to store the JSON response in Model class, when we receive the array of dictionary then we use ” for loop ” for inserting each dictionary data into model object, this way of doing is not good .its better to use Map for inserting the data into model class . for doing this we need to follow this steps:
1: create a Model class like this.
I have taken the example like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class ProductCollectionModel: NSObject { var productName:String = "" var productImage:String = "" var descriptionData:String = "" var id:String = "" var price:String = "" init(data: JSON) { self.productImage = data["thumbNail"].stringValue self.descriptionData = data["shortDescription"].stringValue self.id = data["entityId"].stringValue self.price = data["formatedFinalPrice"].stringValue self.productName = data["name"].stringValue } } |
This is sample model class, which store each dictionary data in this class.
2: Now for ViewModel Class where we make the function to insert the data into the model class.
1 2 3 4 5 6 7 8 9 10 |
class ProductCollectionViewModel { var productCollectionModel = [ProductCollectionModel]() init(data:JSON) { if data["productList"].arrayObject != nil{ let arrayData = data["productList"].arrayObject! as NSArray productCollectionModel = arrayData.map({(value) -> ProductCollectionModel in return ProductCollectionModel(data:JSON(value)) }) } } |
Here “productList” is JSON array whose key is “productList” and it contains an array of the dictionary.
like that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
productList = ( { configurableData = { }; entityId = 2; finalPrice = 32; formatedFinalPrice = "$32.00"; formatedPrice = "$32.00"; formatedSpecialPrice = "$32.00"; hasOptions = 0; hasTierPrice = 0; isAvailable = 1; isInRange = 1; isInWishlist = 0; name = "Strive Shoulder Pack"; price = "32.0000"; rating = "4.50"; requiredOptions = 0; shortDescription = "Convenience is next to nothing when your day is crammed with action. So whether you're heading to class, gym, or the unbeaten path, make sure you've got your Strive Shoulder Pack stuffed with all your essentials, and extras as well. \n \nZippered main compartment. \nFront zippered pocket. \nSide mesh pocket. \nCell phone pocket on strap. \nAdjustable shoulder strap and top carry handle. \n"; sku = "24-MB04"; specialPrice = "32.0000"; thumbNail = "http://192.168.15.107/m2mo2/pub/media/catalog/product/cache/image/256x/beff4985b56e3afdbeabfc89641a4582/m/b/mb04-black-0.jpg"; typeId = simple; wishlistItemId = 0; }, { configurableData = { }; entityId = 10; finalPrice = "24.0000"; formatedFinalPrice = "$24.00"; formatedPrice = "$32.00"; formatedSpecialPrice = "$24.00"; hasOptions = 0; hasTierPrice = 0; isAvailable = 1; isInRange = 1; isInWishlist = 0; name = "Savvy Shoulder Tote"; price = "32.0000"; rating = "4.00"; requiredOptions = 0; shortDescription = "Powerwalking to the gym or strolling to the local coffeehouse, the Savvy Shoulder Tote lets you stash your essentials in sporty style! A top-loading compartment provides quick and easy access to larger items, while zippered pockets on the front and side hold cash, credit cards and phone.\n\nWater-resistant shell.\nWater bottle pocket.\nPadded, articulating shoulder strap.\nDimensions: W 21\" x H 15\" x D 10\".\n"; sku = "24-WB05"; specialPrice = "24.0000"; thumbNail = "http://192.168.15.107/m2mo2/pub/media/catalog/product/cache/image/256x/beff4985b56e3afdbeabfc89641a4582/w/b/wb05-red-0.jpg"; typeId = simple; wishlistItemId = 0; }, { configurableData = { }; entityId = 11; finalPrice = 33; formatedFinalPrice = "$33.00"; formatedPrice = "$33.00"; formatedSpecialPrice = "$33.00"; hasOptions = 0; hasTierPrice = 0; isAvailable = 1; isInRange = 1; isInWishlist = 0; name = "Endeavor Daytrip Backpack"; price = "33.0000"; rating = "3.67"; requiredOptions = 0; shortDescription = "With more room than it appears, the Endeavor Daytrip Backpack will hold a whole day's worth of books, binders and gym clothes. The spacious main compartment includes a dedicated laptop sleeve. Two other compartments offer extra storage space.\n\nFoam-padded adjustable shoulder straps.\n900D polyester.\nOversized zippers.\nLocker loop.\n"; sku = "24-WB06"; specialPrice = "33.0000"; thumbNail = "http://192.168.15.107/m2mo2/pub/media/catalog/product/cache/image/256x/beff4985b56e3afdbeabfc89641a4582/w/b/wb06-red-0.jpg"; typeId = simple; wishlistItemId = 0; }, { configurableData = { }; entityId = 16; finalPrice = 12; formatedFinalPrice = "$12.00"; formatedPrice = "$12.00"; formatedSpecialPrice = "$12.00"; hasOptions = 0; hasTierPrice = 0; isAvailable = 1; isInRange = 1; isInWishlist = 0; name = "Dual Handle Cardio Ball"; price = "12.0000"; rating = "5.00"; requiredOptions = 0; shortDescription = "Make the most of your limited workout window with our Dual-Handle Cardio Ball. The 15-lb ball maximizes the effort-impact to your abdominal, upper arm and lower-body muscles. It features a handle on each side for a firm, secure grip.\n\nDurable plastic shell with sand fill.\nTwo handles.\n15 lbs.\n"; sku = "24-UG07"; specialPrice = "12.0000"; thumbNail = "http://192.168.15.107/m2mo2/pub/media/catalog/product/cache/image/256x/beff4985b56e3afdbeabfc89641a4582/u/g/ug07-bk-0.jpg"; typeId = simple; wishlistItemId = 0; }, { configurableData = { }; entityId = 41; finalPrice = 43; formatedFinalPrice = "$43.00"; formatedPrice = "$43.00"; formatedSpecialPrice = "$43.00"; hasOptions = 0; hasTierPrice = 0; isAvailable = 1; isInRange = 1; isInWishlist = 0; name = "Luma Analog Watch"; price = "43.0000"; rating = "4.00"; requiredOptions = 0; shortDescription = "Designed to stand up to your active lifestyle, this women's Luma Analog Watch features a tasteful brushed chrome finish and a stainless steel, water-resistant construction for lasting durability.\n\nPrecision Miyota\U00ae three-hand movement.\n"; sku = "24-WG09"; specialPrice = "43.0000"; thumbNail = "http://192.168.15.107/m2mo2/pub/media/catalog/product/cache/image/256x/beff4985b56e3afdbeabfc89641a4582/w/g/wg09-gr-0.jpg"; typeId = simple; wishlistItemId = 0; }, { configurableData = { }; entityId = 42; finalPrice = 49; formatedFinalPrice = "$49.00"; formatedPrice = "$49.00"; formatedSpecialPrice = "$49.00"; hasOptions = 0; hasTierPrice = 0; isAvailable = 1; isInRange = 1; isInWishlist = 0; name = "Bolo Sport Watch"; price = "49.0000"; rating = "3.33"; requiredOptions = 0; shortDescription = "The Bolo Sport Watch is sleek, sporty and sized just right to fit your smaller wrist. Easy to read and set up, it features a large digital face and button-activated alarm and stopwatch. The soft-touch resin band promises no-pinch comfort, while the water-resistant design lets you take your workout to the lap pool. \n \nDisplays time, day and date. \nTwo-tone design. \n12/24 hour formats. \nNickel-free buckle on band. \nBattery included. \n"; sku = "24-WG01"; specialPrice = "49.0000"; thumbNail = "http://192.168.15.107/m2mo2/pub/media/catalog/product/cache/image/256x/beff4985b56e3afdbeabfc89641a4582/w/g/wg01-bk-0.jpg"; typeId = simple; wishlistItemId = 0; } ); |
3: Now using this function you can access the model class as an array.
1 2 3 |
var getProductCollectionData:Array<ProductCollectionModel>{ return productCollectionModel } |