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

时间:2019-04-13 11:43:43

标签: arrays swift alamofire indexof swifty-json

您好,我是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."
}

现在您可以看到我的回复,我只想检索下面的前两个索引值

预期产量

用于第一索引预期输出

{
          "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

请查看我的代码,我正在按预期方式获得输出,但是当响应时只有一个数据,那么我在第二个索引上崩溃了,因为第二个索引上没有数据 请有人帮我

2 个答案:

答案 0 :(得分: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 :(得分: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")
        }
    }

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