簡體   English   中英

如何在Swift 3中將數據附加到來自服務器響應的字典中?

[英]How to append the data to dictionary coming from server response in swift 3?

在這里,我收到服務器數據的響應,但是在這里,我需要顯示如下運輸方法部分中的圖像所示,在此承運人標題中將包含子方法,在該子方法中,該子方法需要按節中的行數顯示,並顯示節標題承運人標題將被命名,並且此處的方法標題需要從具有相同名稱的特定運營商標題中追加,任何人都可以幫助我實現該方法嗎?

我已經嘗試過的代碼是

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()
    }

來自服務器的Json響應是

[
      {
        "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
      }
]

你可以做這樣的事情

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)
}

輸出量

["Flat Rate": ["Fixed"], "Best Way": ["Table Rate", "Worldwide Expedited", "Worldwide Express Saver"]]

更新

要獲取所有值的計數,請執行以下操作

var count = 0
for (key, value) in finalDict {
    count += value.count
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM