0ndre_ 0ndre_ - 2 months ago 7
iOS Question

ParseJson data from array located in another array

Hello I'm trying to parseJson data from an array. My problem is that I do not know how to access my response , because it is an array located in another array.

This is my response:

{"finalResult":
[
[
{"Id":2,"latitude":"49.993820099043","longitude":"14.660662520439","usrId":2,"timeCreated":"2016-08-06 18:31:32"}],
[{"Id":3,"latitude":"49.993900553360","longitude":"14.660832398337","usrId":2,"timeCreated":"2016-08-06 18:29:37"}]
]
}


This is part of code I need help with:

if let parseJson = json {

if let events = parseJson["finalResult"] as? [AnyObject] {

for event in events {

for single in (event as? [AnyObject])! {
let Id = single["Id"] as! String
let latitude = single["latitude"] as! String
let longitude = single["longitude"] as! String
let usrId = single["usrId"] as! String

print(id)
print(latitude)
print(longitude)
print(usrId)
}
}
}
}


Thank you very much.

Answer
for event in events  {
    if let customObjects = event as? [AnyObject] {
        for customObject in customObjects {
            let Id = customObject["Id"] as! String
            let latitude = customObject["latitude"] as! String
            let longitude = customObject["longitude"] as! String
            let usrId = customObject["usrId"] as! String

            print(id)
            print(latitude)
            print(longitude)
            print(usrId)
        }
    }
}
Comments