ios - how to get the count for the given Json response in swift 3? -


in api getting following data , in need total names count in arrays

{   "flat": [     {       "price": "$5.00",        "id": 11,        "name": "fixed"     }   ],    "united parcel service": [     {       "price": "$109.12",        "id": 1,        "name": "worldwide expedited"     },      {       "price": "$120.18",        "id": 2,        "name": "worldwide express saver"     }   ] } 

i had tried following code names count in arrays

    var arrayss =  [string:anyobject]()     var keys = [string]()     let urlstring = "http://www.json-generator.com/api/json/get/bvgbyvqgmq?indent=2"     var totalcount = 0 func shippingmethodurl() {         let url = nsurl(string: self.urlstring)         urlsession.shared.datatask(with: (url url?)!, completionhandler: {(data, response, error) -> void in             if let jsonobj = try? jsonserialization.jsonobject(with: data!, options: .allowfragments) as? nsdictionary {                 self.arrayss = jsonobj as! [string : anyobject]                 self.keys = jsonobj?.allkeys as! [string]                 operationqueue.main.addoperation({                     self.shippingtableview.reloaddata()                     let sectionheight = self.arrayss.count * 31                     let cellheight = self.keys.count * 44                     self.shippingheightconstraint.constant = cgfloat(sectionheight + cellheight)                     self.heightconstant = int(self.shippingheightconstraint.constant)                     self.delegate?.heightconstant(int: self.heightconstant!)                 })             }         }).resume()     } 

first of all, don't use foundation data types when have native swift equivalents (such nsurl , nsdictionary).

other that, problem counting keys of dictionary. however, want iterate through dictionary, use conditional casting check if value associated key array , if array, check if dictionary elements of array have "name" key, if do, increment count, otherwise nothing.

also don't use force unwrapping , force casting when parsing json data, since in network response there real possibility can fail , app crash.

func shippingmethodurl() {     guard let url = url(string: self.urlstring) else {return}     urlsession.shared.datatask(with: url, completionhandler: {(data, response, error) -> void in         if let data = data, let jsonobj = (try? jsonserialization.jsonobject(with: data, options: .allowfragments)) as? [string:anyobject] {             self.arrayss = jsonobj             self.keys = array(jsonobj.keys)              var totalcount = 0             value in jsonobj.values {                 if let array = value as? [[string:any]] {                     element in array {                         if let name = element["name"] as? string {                             totalcount += 1                         }                     }                 }             }             //update ui , other variables here         }     }).resume() } 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -