【发布时间】:2020-03-15 07:46:02
【问题描述】:
我的 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\"}]",
}
在这里,我在 tableview 中得到了 3 次 Municipal Corporation - Water,我需要它一次,并且它的 paramName 在 waterParamArray 中应该是 3。同样,如果在 waterParamArray 中有一个 paramName,则有一个值。
代码如下:
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
}
这里的 textFieldList 是 nextVCs var textFieldList = [String](),因此它包含多少个 paramName 将返回该计数
但一直 textFieldList 包含单个值,为什么?甚至 json 包含三个 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)
}
}
}
请在上面的代码中帮助我。
【问题讨论】:
-
与您之前的类似问题一样,仅获得代码片段的帮助非常困难。评论minimal reproducible example。
-
你得到的所有参数值是相同的还是只是名称?
-
@Patel123
Municipal Corporation - Water包含三个参数名称,所以我在 tableview 中获得了三倍市政公司 - 水,但我需要一次市政公司 - 水`,如果我选择所有三个参数名称添加到waterParamArray .......请帮我解决我卡在这里很久的代码 -
id 保持不变??
-
@Patel123,
for item in res {}这行的 bcoz 我得到了三倍Municipal Corporation - Water
标签: ios json swift uitableview dictionary