Home How to filter according to element in json in swift 3?
Reply: 0

How to filter according to element in json in swift 3?

user2042
1#
user2042 Published in May 23, 2018, 6:54 pm

I have an array like this in which if I got value of 57 then I need to append numbers corresponding to 175..etc. If stock for this is 1 it should need to append and if stock 0 then the value should not append to the array. Can anyone show me how to implement this in swift 3 ?

Here is my code which I already have tried previously:

for item in (self.detailModel?.extensionAttribute?.productStock)! {
    //                            let jsonStr = "{\"label\":\"57-175\",\"stock\":0}"
    let dict = try! JSONSerialization.jsonObject(with: item.data(using: .utf8)!, options: []) as! [String:Any]
    let labelValue = dict["label"] as! String
    print(labelValue)
    let values:[String] = labelValue.components(separatedBy: "-")
    print(values)
    self.colorNumber = Int(values[0])
    self.sizeNumber = Int(values[1])
    let stock = dict["stock"] as! Int
    let value = conditionCheck
    if value == self.colorNumber {
        if stock != 0 {
            self.sizeArray.append(self.sizeNumber!)
            print(self.sizeArray)
        }
    }
}

Here is my array of json data:

"configurable_product_options_stock": [ "{\"label\":\"57-175\",\"stock\":0}", "{\"label\":\"60-175\",\"stock\":1}", "{\"label\":\"64-175\",\"stock\":1}", "{\"label\":\"57-176\",\"stock\":1}", "{\"label\":\"60-176\",\"stock\":1}", "{\"label\":\"64-176\",\"stock\":1}", "{\"label\":\"57-177\",\"stock\":1}", "{\"label\":\"60-177\",\"stock\":1}", "{\"label\":\"64-177\",\"stock\":1}", "{\"label\":\"57-178\",\"stock\":1}", "{\"label\":\"60-178\",\"stock\":1}", "{\"label\":\"64-178\",\"stock\":1}", "{\"label\":\"57-179\",\"stock\":1}", "{\"label\":\"60-179\",\"stock\":1}", "{\"label\":\"64-179\",\"stock\":1}" ],

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.324974 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO