迭代数组并从JSON文件

时间:2016-06-06 11:40:24

标签: ios arrays json swift swifty-json

这是我从服务器

收到的JSON

Doubt.JSON

我的问题是在这里我收到两个" imageData"这是Array类型。如何提取每一个并将它们分配给集合视图单元格。正如在第一个imageData中,应该转到一个单元格,接下来就是这样。

我正在使用ALAMOFIRE和SWIFTYJSON。如果有人可以帮助我使用/不使用SwiftyJSON,我真的很感激。

我的JSON:

{
  "response" : {
    "start" : 0,
    "docs" : [
      {
        "enrollmentId" : [
          "2534534246"
        ],
        "fieldName2" : [
          "Languages"
        ],
        "locality" : [
          "XYZ"
        ],
        "active" : [
          true
        ],
        "sex" : [
          "Male"
        ],
        "latitude" : [
          1.2340
        ],
        "city" : [
          "udya"
        ],
        "imageData" : [
          "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEgAAABkCAYAAAAlg3YKAAA1wklEQVR4AcWd95Nc13XnT+c405MHMyACARDMoCxSibYsrWtXP7h2\/1XXbklea4vyWlW2apUp0SbBIJIgEQaYgMk9nXs\/n\/umgUEgCUis8gN7uvv1fffd+70nn3Mfc3fu3BnHf9IxuXHua76\/\/X5dfRbfeeedr3l4X93dZAJjZpFzKvw3OZcf8fn4e+ppPE6THeeO3\/kxAUsbP+QiH\/l0YsxX\/nF+lBtxnn9++QuP4l94\/VdePnaCOQbLa3LkR+PI8523bFJpokyWJuMAISHhx3Qd1xf4YTwaMXFBEqB8DDkfuTxtvSIhNOn+HjDe2+Pkve81esIPXxtATj+N5z4OaTJOoNfrpVen04n9vf3YvXs3Dvb24sbNm7GxtcUE8lEoFWg5juFwwGsYxWIxSoVClHOFmKrVol7le6kS5y9diHPPXohKvZHwGQp0uie0dIxTAvEYnJM4TBbr5Lkv+2z7Pxugyep4g7wrzpGjQwfnivUHg1i\/cydu81pfX48bN67HzRs3Y8Dkh\/y2v7MXnV4nWRqE79z3\/wOcrF6q09MBWgAAAABJRU5ErkJggg=="
        ],
        "fieldValue2" : [
          "English, Hindi, Kannada"
        ],
        "fieldValue1" : [
          "11 years"
        ],
        "state" : [
          "Karnataka"
        ],
        "id" : "Iuusofijpw",
        "email" : [
          "cont@dontcare.com"
        ],
        "longitude" : [
          12.4352
        ],
        "firstName" : [
          "Praqwer"
        ],
        "_version_" : 1521637513434759168,
        "experience" : [
          10
        ],
        "caseTypes" : [
          "A Particular caseType let's say Bheema",

        ],
        "lastName" : [
          "Bsdtty"
        ],
        "mode" : [
          "Lawyer"
        ],
        "fieldName1" : [
          "Experience"
        ]
      },
      {
        "state" : [
          "Karnataka"
        ],
        "languages" : [
          "English",
          "Hindi",
          "Kannada"
        ],
        "lastName" : [
          "KWE"
        ],
        "firstName" : [
          "RANDARTY"
        ],
        "sex" : [
          "Male"
        ],
        "caseTypes" : [
          "Bheema",

        ],
        "enrollmentId" : [
          "4253646"
        ],
        "mobile" : [
          42534346
        ],
        "id" : "Iurweoin",
        "imageData" : [
          "data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFcAAABkCAYAAADzJqxvAAAgAElEQVR4XjS9ebBv2VkdtvY+8zm\/8c5vfj2r1VJrQgNEiMkYi0m2kmAbE3DhlAPEqcSuciquVIrgMhCTgF1BQgSMkW0EthkEGCQQIAQCScgSaqk19Ot+3W++872\/8Yz7nL1T6zvP\/Qelbu793XP28H3rW2t9308d3P2iaxEggIdivcBgmCDQPirTwA8UqlIhiDV8pdB0JZz2AevBg4+mrgHTAoHGMElR1DX8MIJvLKzXojMafgg0dQMFwPM9dI2D8g06aHidD4sWjXYAFBLto1WAcw3asoNrLaxSsF0vLKiqrrJc3fu6t12JWqm5qdnbeWJUJF8OipsQ8UJGq6cvVNjU9MWIbHQUeNhbJxGDSYVn0TPQUv4GxJKG0Skpv+tuHWBqbXQ6WSyOjA\/VtRCUqZYrH4UUv2\/ziJ\/f\/aiwX2jbsXKz3ga\/aN+4CG28vb9o27Fys94Gv2jfuAhtvL2\/aNuxcrPeBr\/g2M2m9YyIB6mAAAAABJRU5ErkJggg=="
        ],
        "active" : [
          true
        ],
        "mode" : [
          "Kelsa"
        ],
        "city" : [
          "Udya"
        ],
        "_version_" : 15216,
        "email" : [
          "Him@somenone.com"
        ],
        "experience" : [
          18223
        ],
        "locality" : [
          "XYZ"
        ]
      }
    ],
    "numFound" : 2
  },
  "responseHeader" : {
    "status" : 0,
    "QTime" : 5,
    "params" : {
      "q" : "mode:(\"Lawyer\", \"CA\") AND active:true AND city:(\"Bereello\") AND locality:(\"XYZ\") AND caseTypes:(\"Momerty\")",
      "wt" : "json"
    }
  }
}

我的代码:

Alamofire.request(.GET, url!)
    .validate()
    .responseJSON{ response1 in
        switch response1.result{
        case .Success:
            let json33 = JSON(data: response1.data!)

            print(json33)
            if let userDict = json33["response"].dictionary{

                if let array = userDict["docs"]?.array{

                    for imgd in array{

                        let me:String! = imgd["imageData"][0].string

                        let url = NSURL(string: me)!
                        let data = NSData(contentsOfURL: url)!
                        let image = UIImage(data: data)
                        let imgView = UIImageView(image: image!)
                        imgView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
                        self.view.addSubview(imgView)
                    }


                }

            }

1 个答案:

答案 0 :(得分:1)

每个词典都有一个图像文本条目,这些词典都在一个数组中。

我会使用flatMap快速获取这些图片字符串:

let allImageStrings = json["response"]["docs"].flatMap { $0.1["imageData"].first?.1 }

说明:我们迭代json["response"]["docs"]并访问元组的第二部分(记住,SwiftyJSON对象总是有(index, value))并从那里我们取第一个元组的第二部分[ "为imageData"。]

它看起来很复杂,但当你看到JSON结构时,它实际上相当清晰。