Here I am having response from server data but here I need to display as shown in image below in shipping method section which in this carrier title will have sub methods in which it need to display in number of rows in section and for section title carrier title will be named and here method title needs to be appended from a particular carrier title with same names can anyone help me how to implement this ?
The code already I tried is
var doubleRemoving : [String:Any] = [:]
do
{
let array = try JSONSerialization.jsonObject(with: data, options: []) as? [[String : Any]]
self.responseData = array!
print(self.responseData)
}
catch let error
{
print("json error:", error)
}
for item in self.responseData {
let dict = item
let array = dict["carrier_title"]
self.keyString.append(array as! String)
self.doubleRemoving.updateValue(0, forKey: array as! String)
print(self.doubleRemoving)
}
for item in self.responseData{
if self.doubleRemoving.keys.contains(item["carrier_title"] as! String) {
self.doubleRemoving.updateValue(item["method_title"]!, forKey: item["carrier_title"] as! String)
}
print(self.doubleRemoving)
}
let status = (response as! HTTPURLResponse).statusCode
self.keyStatusCode = status
print(status)
}
task.resume()
}
The Json response from server is
[
{
"carrier_code": "flatrate",
"method_code": "flatrate",
"carrier_title": "Flat Rate",
"method_title": "Fixed",
"amount": 0,
"base_amount": 0,
"available": true,
"error_message": "",
"price_excl_tax": 0,
"price_incl_tax": 0
},
{
"carrier_code": "tablerate",
"method_code": "bestway",
"carrier_title": "Best Way",
"method_title": "Table Rate",
"amount": 0,
"base_amount": 0,
"available": true,
"error_message": "",
"price_excl_tax": 0,
"price_incl_tax": 0
},
{
"carrier_code": "tablerate",
"method_code": "bestway",
"carrier_title": "Best Way",
"method_title": "Worldwide Expedited",
"amount": 0,
"base_amount": 0,
"available": true,
"error_message": "",
"price_excl_tax": 0,
"price_incl_tax": 0
},
{
"carrier_code": "tablerate",
"method_code": "bestway",
"carrier_title": "Best Way",
"method_title": "Worldwide Express Saver",
"amount": 0,
"base_amount": 0,
"available": true,
"error_message": "",
"price_excl_tax": 0,
"price_incl_tax": 0
}
]
You can do something like this
var finalDict = [String: [String]]()
let array = try JSONSerialization.jsonObject(with: getData()!, options: []) as! NSArray
for item in array {
let dict = item as! NSDictionary
let carrierTitle = dict["carrier_title"] as! String
let methodTitle = dict["method_title"] as! String
if finalDict[carrierTitle] == nil {
finalDict[carrierTitle] = [String]()
}
finalDict[carrierTitle]!.append(methodTitle)
}
Output
["Flat Rate": ["Fixed"], "Best Way": ["Table Rate", "Worldwide Expedited", "Worldwide Express Saver"]]
UPDATE
To get the count of all the values, do the following
var count = 0
for (key, value) in finalDict {
count += value.count
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.