Why one bname value showing its paramName count times in tableview while parsing in swift?

戏子无情 提交于 2020-01-17 17:48:04

问题


my JSON:

  {
  "id": "70",
  "bname": "Municipal Corporation - Water",
  "bcategoryname": "Water",
  "bcustomerparms": "[{\"paramName\":\"Consumer Number\",\"dataType\":\"NUMERIC\",\"optional\":\"false\",\"minLength\":\"1\",\"maxLength\":\"10\"},
               {\"paramName\":\"Mobile Number\",\"dataType\":\"NUMERIC\",\"optional\":\"false\",\"minLength\":\"10\",\"maxLength\":\"10\"},
               {\"paramName\":\"Email . Id\",\"dataType\":\"ALPHANUMERIC\",\"optional\":\"false\",\"minLength\":\"5\",\"maxLength\":\"100\"}]",
  }
  "id": "68",
  "bname": "Municipal Corporation - 12",
  "bcategoryname": "Water",
  "bcustomerparms": "[{\"paramName\":\"K No\",\"dataType\":\"ALPHANUMERIC\",\"optional\":\"false\",\"minLength\":\"7\",\"maxLength\":\"20\"}]",
  } 

here i am getting Municipal Corporation - Water three times in tableview, i need it one time and its paramName should be three in waterParamArray. like wise if there is one paramName then one value in waterParamArray.

here is the code:

    do{
    let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
    //print("the all make payment json is \(jsonObj)")
    let billerdetailsArray = jsonObj["billerdetails"] as! [[String: Any]]

    for billerdetail in billerdetailsArray {

        self.categoryName = billerdetail["bcategoryname"] as? String

        let customrParams = billerdetail["bcustomerparms"] as! String
        let res = try JSONSerialization.jsonObject(with:Data(customrParams.utf8)) as! [[String: Any]]

        for item in res {
            self.paramName = item["paramName"] as? String
            if self.categoryName == "Water"{
                let bName = billerdetail["bname"] as? String
                self.waterArray.append(bName ?? "")
                self.waterParamArray.append(self.paramName ?? "")
            }

            if self.categoryName == "Electricity"{
                let bName = billerdetail["bname"] as? String
                self.electricityArray.append(bName ?? "")
                self.electrictyParamArray.append(self.paramName ?? "")
            }
        }
    }
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    guard let category = category else {return 0}
    switch category {
    case .electricity: return electricityArray.count
    case .water: return waterArray.count
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! PaymentTableViewCell

    guard let category = category else {return cell}
    switch category {
    case .electricity: cell.pamntTypeLabel.text = electricityArray[indexPath.row]
    case .water: cell.pamntTypeLabel.text = waterArray[indexPath.row]

    }
    return cell
}

here textFieldList is nextVCs var textFieldList = [String](), so that how many paramName it contains it will return that count

but all the time textFieldList contains single value why? even json contains three paramNames

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath) as? PaymentTableViewCell

if let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "TextFiViewController") as? TextFiViewController
{
    switch category {
    case .electricity?: nextViewController.textFieldList = [electrictyParamArray[indexPath.row]]//electrictyParamArray[indexPath.row]

    case .water?: nextViewController.textFieldList = [waterParamArray[indexPath.row]]
    case .none:
        print("in didselect switch")
    }
    self.navigationController?.pushViewController(nextViewController, animated: true)
}
}
}

please help me in the above code.


回答1:


try this

struct MunicipalCorporation : Decodable{
    var id : String
    var bname : String
    var bbcategoryname : String
    var bcustomerparms : [Bcustomerparms]
}

struct Bcustomerparms : Decodable {
    var paramName : String
    var dataType : String
    var optional :  Bool?
    var minLength : Int
    var maxLength : Int
}
let jsonObj = try JSONSerialization.jsonObject(with: respData, options: .allowFragments) as! [String: Any]
let decoding = try! JSONDecoder().decode(MunicipalCorporation.self, from: jsonObj)


来源:https://stackoverflow.com/questions/58939991/why-one-bname-value-showing-its-paramname-count-times-in-tableview-while-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!