如何将嵌套的JSON数据数组加载到iOS Swift中?

我已经嵌套了JSON响应数据,并且正在尝试将数据加载到集合视图单元格中。每个单元格包含受让人,名字,贷款额。

每当集合视图加载单元格的名字和加载量更改或将值放入包含空值的单元格时,我的问题。

例如:-

单元格1-响应数据:受让人-演示,名字-光环和贷款额-3000

单元2-响应数据:受让人-demo1,名字-David和贷款额-2000

单元格3-响应数据:受让人-演示,名字-空,贷款金额-空

在我的情况下,当第一次收集单元格正确装入但移至另一个控制器并返回到收集视图控制器单元格3时,其名称和加载量已填充了名称和加载量的单元格2值,但应为空。

因为在我的API响应数据中,名字和姓氏有时会为空。

这是我儿子的回复数据:

      {
"_links": {
"self": {
    "href": "/task"
  }
 },
 "_embedded": {

 "task": [
    {
        "_links": {
            "assignee": {
                "href": "/user/demo"
            },
            "execution": {
                "href": "/execution/1b64cf75-0616-11ea-8860-120ef5ab2c25"
            },
            "identityLink": {
                "href": "/task/1b64f688-0616-11ea-8860-120ef5ab2c25/identity-links"
            },
            "processDefinition": {
                "href": "/process-definition/quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25"
            },
            "processInstance": {
                "href": "/process-instance/1b64cf75-0616-11ea-8860-120ef5ab2c25"
            },
            "self": {
                "href": "/task/1b64f688-0616-11ea-8860-120ef5ab2c25"
            }
        },
        "_embedded": {
            "variable": []
        },
        "id": "1b64f688-0616-11ea-8860-120ef5ab2c25",
        "name": "Quick Evaluation",
        "assignee": "demo",
        "created": "2019-11-13T13:04:20.687+0000",
        "due": null,
        "followUp": null,
        "delegationState": null,
        "description": null,
        "executionId": "1b64cf75-0616-11ea-8860-120ef5ab2c25",
        "owner": null,
        "parentTaskId": null,
        "priority": 50,
        "processDefinitionId": "quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25",
        "processInstanceId": "1b64cf75-0616-11ea-8860-120ef5ab2c25",
        "taskDefinitionKey": "QuickEvaluation",
        "caseExecutionId": null,
        "caseInstanceId": null,
        "caseDefinitionId": null,
        "suspended": false,
        "formKey": "a8apps:suryoday:gng:v0.1.0:kycUpload",
        "tenantId": null
    },
    {
        "_links": {
            "assignee": {
                "href": "/user/demo"
            },
            "execution": {
                "href": "/execution/412a03b7-06ae-11ea-8860-120ef5ab2c25"
            },
            "identityLink": {
                "href": "/task/412a2aca-06ae-11ea-8860-120ef5ab2c25/identity-links"
            },
            "processDefinition": {
                "href": "/process-definition/quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25"
            },
            "processInstance": {
                "href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25"
            },
            "self": {
                "href": "/task/412a2aca-06ae-11ea-8860-120ef5ab2c25"
            }
        },
        "_embedded": {
            "variable": [
                {
                    "_links": {
                        "self": {
                            "href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25/variables/loanAmount"
                        }
                    },
                    "_embedded": null,
                    "name": "loanAmount",
                    "value": "650000",
                    "type": "String",
                    "valueInfo": {}
                },
                {
                    "_links": {
                        "self": {
                            "href": "/process-instance/412a03b7-06ae-11ea-8860-120ef5ab2c25/variables/firstName"
                        }
                    },
                    "_embedded": null,
                    "name": "firstName",
                    "value": "Kamesh",
                    "type": "String",
                    "valueInfo": {}
                }
            ]
        },
        "id": "412a2aca-06ae-11ea-8860-120ef5ab2c25",
        "name": "Quick Evaluation",
        "assignee": "demo",
        "created": "2019-11-14T07:13:27.558+0000",
        "due": null,
        "followUp": null,
        "delegationState": null,
        "description": null,
        "executionId": "412a03b7-06ae-11ea-8860-120ef5ab2c25",
        "owner": null,
        "parentTaskId": null,
        "priority": 50,
        "processDefinitionId": "quickEvaluation:1:129ce2b1-0616-11ea-8860-120ef5ab2c25",
        "processInstanceId": "412a03b7-06ae-11ea-8860-120ef5ab2c25",
        "taskDefinitionKey": "QuickEvaluation",
        "caseExecutionId": null,
        "caseInstanceId": null,
        "caseDefinitionId": null,
        "suspended": false,
        "formKey": "a8apps:suryoday:gng:v0.1.0:kycUpload",
        "tenantId": null
       }

      ]
     },
   "count": 13
    }

现在我们可以看到_embedded“和” variable“包含一个值数组,例如name =名字和借贷金额。但是,在另一个数据中,_embedded”&“ variable”包含空值。

这是我的收藏视图代码:

   public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TaskListCollectionViewCell", for: indexPath) as! TaskListCollectionViewCell

            cell.layer.cornerRadius = 10
            let shadowPath2 = UIBezierPath(rect: cell.bounds)
            cell.layer.masksToBounds = false
            cell.layer.shadowColor = UIColor(hexString: "#a8abbd")?.withAlphaComponent(0.3).cgColor
            cell.layer.shadowOffset = CGSize(width: CGFloat(0.0), height: CGFloat(5.0))

            cell.layer.shadowOpacity = 0.20
            cell.layer.shadowPath = shadowPath2.cgPath

     //   let sortedData = self.myTaskAry.sorted{ $0.created > $1.created }



       // let t = sortedData[indexPath.row]
    let t = self.myTaskAry[indexPath.row]

    print("t name::", t.name)
    print("t assignee:::", t.assignee)

        cell.tagName?.text = t.name

        cell.assigneeName?.text = t.name

    if t._embedded?.variable?[indexPath.row].name == "loanAmount" {

        print("t loanamount:::", t._embedded?.variable?[indexPath.row].value)

        cell.variablevalue1.text = t._embedded?.variable?[indexPath.row].value

    } else {
        cell.variablevalue1.text = "NA"
    }

       if t._embedded?.variable?[indexPath.row].name == "firstName" {

         print("t firstname:::", t._embedded?.variable?[indexPath.row].value)

            cell.variablevalue2.text = t._embedded?.variable?[indexPath.row].value
       } else {
                  cell.variablevalue2.text = "NA"
              }





  //        for val in (t._embedded?.variable)!{
  //
   //            if val.name == "loanAmount" {
  //
   //                print("t loanamount:::", val.value)
  //
  //                cell.variablevalue1.text = val.value
   //                    }
   //
  //               if val.name == "firstName" {
  //
  //                 print("t firstname:::", val.value)
   //
   //                   cell.variablevalue2.text = val.value
   //               }
   //
  //        }

  //        print("task variable array", self.taskVariableValues)
  //        print("task variable array count", self.taskVariableValues.count)

   //
        let formato = DateFormatter()
        formato.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        formato.timeZone = NSTimeZone.local
        formato.formatterBehavior = .default
        let data = formato.date(from: t.created)
        formato.dateFormat = "dd-MM-yyyy"
    //            print("data::\(String(describing: getPastTime(for: data!)))")

        cell.dateCreated?.text = getPastTime(for: data!)
        cell.tagName?.textColor = UIColor(hexString: "#00bdff")
        cell.tagView.backgroundColor = UIColor(hexString: "#00BDFF")?.withAlphaComponent(0.25)
   //          t.processInstanceId
        print(t._embedded)
        return cell

    }

预期结果:

单元格1-响应数据:受让人-演示,名字-光环和贷款额-3000

单元2-响应数据:受让人-demo1,名字-David和贷款额-2000

单元格3-响应数据:受让人-演示,名字-空,贷款金额-空

单元格4-响应数据:受让人-演示,名字-空,贷款金额-空

单元格5-响应数据:受让人-演示,名字-Jeniffer和贷款额-4500

任何帮助非常感谢...

评论