从json响应中获取前两个索引的数据

问题描述 投票:0回答:2

您好我是swift的新手,我通过AlamofireSwiftyJON调用API,我能够成功地检索响应但是从那个数据我想只检索前两个数据索引让我简要解释一下

这是我的回复

{
  "previous_inspection_list" : [
    {
      "inspection_number" : "3",
      "date_inspected" : "2019-04-13",
      "fk_properties_id" : "2",
      "created_by" : "3",
      "signature" : "img_ZbsOx3fx1r1555154350.png",
      "status" : "1",
      "fk_user_id" : "3",
      "signature_name" : "Vishal Parmar",
      "created_date" : "2019-04-13 05:19:10",
      "updated_by" : "0",
      "inspections_id" : "8",
      "updated_date" : "0000-00-00 00:00:00"
    },
    {
      "inspection_number" : "2",
      "date_inspected" : "2019-04-13",
      "fk_properties_id" : "2",
      "created_by" : "3",
      "signature" : "img_uVQw3K4pfY1555140089.png",
      "status" : "1",
      "fk_user_id" : "3",
      "signature_name" : "Vishal Parmar",
      "created_date" : "2019-04-13 01:21:29",
      "updated_by" : "0",
      "inspections_id" : "6",
      "updated_date" : "0000-00-00 00:00:00"
    },
    {
      "inspection_number" : "1",
      "date_inspected" : "2019-04-13",
      "fk_properties_id" : "2",
      "created_by" : "2",
      "signature" : "img_g6GrjsofPE1555137646.png",
      "status" : "1",
      "fk_user_id" : "2",
      "signature_name" : "Mihir Panchasara",
      "created_date" : "2019-04-13 00:40:46",
      "updated_by" : "0",
      "inspections_id" : "3",
      "updated_date" : "0000-00-00 00:00:00"
    }
  ],
  "success" : "1",
  "message" : "Successfully."
}

因为你现在能够看到我的回复我只想检索前两个索引值,如下所示

期待OutPut

对于第一指数预期产出

{
          "inspection_number" : "3",
          "date_inspected" : "2019-04-13",
          "fk_properties_id" : "2",
          "created_by" : "3",
          "signature" : "img_ZbsOx3fx1r1555154350.png",
          "status" : "1",
          "fk_user_id" : "3",
          "signature_name" : "Vishal Parmar",
          "created_date" : "2019-04-13 05:19:10",
          "updated_by" : "0",
          "inspections_id" : "8",
          "updated_date" : "0000-00-00 00:00:00"
        }

第二指数预期产出

{
      "inspection_number" : "2",
      "date_inspected" : "2019-04-13",
      "fk_properties_id" : "2",
      "created_by" : "3",
      "signature" : "img_uVQw3K4pfY1555140089.png",
      "status" : "1",
      "fk_user_id" : "3",
      "signature_name" : "Vishal Parmar",
      "created_date" : "2019-04-13 01:21:29",
      "updated_by" : "0",
      "inspections_id" : "6",
      "updated_date" : "0000-00-00 00:00:00"
    }

让我告诉你我试过的代码

          let sampleArray = data.array
            let firstdict = sampleArray![0]
            print(firstdict)
            let signature_name = firstdict["signature_name"].stringValue
            let inspection_number = firstdict["inspection_number"].stringValue
            let date_inspected = firstdict["date_inspected"].stringValue
            let inspections_id = firstdict["inspections_id"].stringValue
            self.lblFirstInspName.text = signature_name
            self.lblInspNumber.text = "#\(inspection_number)"
            self.lblFirstInspDate.text = date_inspected
            self.inspID1 = inspections_id

            let secondDict = sampleArray![1]
            let signature_name1 = secondDict["signature_name"].stringValue
            let inspection_number1 = secondDict["inspection_number"].stringValue
            let date_inspected1 = secondDict["date_inspected"].stringValue
            let inspections_id2 = secondDict["inspections_id"].stringValue
            self.lblSeconfInspName.text = signature_name1
            self.lblSecondInspNumber.text = "#\(inspection_number1)"
            self.lblSecondInspDate.text = date_inspected1
            self.inspID2 = inspections_id2

请看我的代码我正在按预期得到输出但是当响应时只有一个数据然后我在第二个索引上崩溃因为第二个索引没有数据请有人帮助我

arrays swift alamofire indexof swifty-json
2个回答
1
投票

在获取第二个之前添加安全检查

if let sampleArray = sampleArray, sampleArray.count > 1 {
    let secondDict = sampleArray[1]
    let signature_name1 = secondDict["signature_name"].stringValue
    let inspection_number1 = secondDict["inspection_number"].stringValue
    let date_inspected1 = secondDict["date_inspected"].stringValue
    let inspections_id2 = secondDict["inspections_id"].stringValue
    self.lblSeconfInspName.text = signature_name1
    self.lblSecondInspNumber.text = "#\(inspection_number1)"
    self.lblSecondInspDate.text = date_inspected1
    self.inspID2 = inspections_id2
}

1
投票

试试这个功能。我试图把重点放在代码中。

func analysis() {
        let rawData = data as! [String: Any] // Specify value type -> By doing this, you specify the type of value that is "JSON"

        let arrayPreviousInspectionList = rawData["previous_inspection_list"] as! [[String: String]] // Getting "previous_inspection_list" and specifying its type -> it's list of [String: String]

        if(arrayPreviousInspectionList.count >= 2) { // You must check the number of objects in the list
            // first index
            let firstInspection = arrayPreviousInspectionList[0]
            let signature_name = firstInspection["signature_name"]!
            let inspection_number = firstInspection["inspection_number"]!
            let date_inspected = firstInspection["date_inspected"]!
            let inspections_id = firstInspection["inspections_id"]!
            self.lblFirstInspName.text = signature_name
            self.lblInspNumber.text = "#\(inspection_number)"
            self.lblFirstInspDate.text = date_inspected
            self.inspID1 = inspections_id


            // second index
            let secondInspection = arrayPreviousInspectionList[1]
            let signature_name1 = secondInspection["signature_name"]!
            let inspection_number1 = secondInspection["inspection_number"]!
            let date_inspected1 = secondInspection["date_inspected"]!
            let inspections_id2 = secondInspection["inspections_id"]!
            self.lblSeconfInspName.text = signature_name1
            self.lblSecondInspNumber.text = "#\(inspection_number1)"
            self.lblSecondInspDate.text = date_inspected1
            self.inspID2 = inspections_id2

        } else {
            print("--- The number of inspections is less than 2")
        }
    }

希望有用。对我的英语也很抱歉。

© www.soinside.com 2019 - 2024. All rights reserved.