Dershowitz123 Dershowitz123 - 6 months ago 20
Swift Question

Iterating over arrays and extracting data from JSON file

Here is a JSON that I'm receiving from the server

Doubt.JSON

My question is here I'm receiving two "imageData" which is of type Array. How do I extract each one of those and assign them to let's say collection view cells. As in 1st imageData should go to one cell and next so on.

I'm using ALAMOFIRE and SWIFTYJSON. If anybody could help me out using/ not using SwiftyJSON, I'd really appreciate it.

My 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"
}
}
}


My code:

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)
}


}

}

Answer

Each one of your dictionaries has one entry for the image text, and these dictionaries are in an array.

I would use flatMap to quickly get these image strings:

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

Explanation: we iterate over json["response"]["docs"] and we access the second part of the tuple (remember, a SwiftyJSON object always has (index, value)) and from there we take the second part of the first tuple inside ["imageData"].

It looks complex like that but when you look at the JSON structure it's actually rather clear.