I would like to obtain specific JSON response. For example, obtaining only the results (i.e. name and score) in the array with id 25 and 26. Is there like a WHERE
clause in swift? I'm using Alamofire and SwiftyJSON if that helps.
Right now my code just pulls in all the values but I want to only have the results with id 25 and 26. Is that possible?
My code:
Alamofire.request(.GET, endPoint).validate()
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
for (_,subJson):(String, JSON) in json {
if let date = subJson["start_date"].string{
self.date = date
}
if let building = subJson["building_name"].string{
self.building = building
}
if let jobId = subJson["schedule_job_id"].int {
self.jobIdArray.append(jobId)
}
if let tasks = subJson["tasks"].array{
for building in tasks {
Tasks.sharedInstance.datas = tasks
if let ratings = building["safety_ratings"].array{
for submission in ratings {
if let score = submission["score"].string {
self.scoreArray.append(score)
}
if let ratingId = submission["safety_rating_id"].int {
self.ratingArray.append(ratingId)
}
if let submissionId = submission["submission_id"].int {
self.subIdArray.append(submissionId)
}
}
}
}
}
}
onCompletion()
}
case .Failure(let error):
print("Request failed with error: \(error)")
onError?(error)
}
}
UPDATE
func getTask(onCompletion: () -> (), onError: ((NSError) -> ())? = nil) {
guard let endPoint = Data.sharedInstance.weeklyEndpoint else { print("Empty endpoint"); return }
Alamofire.request(.GET, endPoint, encoding: .JSON)
.validate()
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
for (_,subJson):(String, JSON) in json {
if let date = subJson["start_date"].string{
self.date = date
}
if let building = subJson["building_name"].string{
self.building = building
}
if let jobId = subJson["schedule_job_id"].int {
self.ratingArray.append(jobId)
}
let IDsYouWant: [Int] = [18, 27]
let tasksYouWant = subJson["tasks"].arrayValue.filter {IDsYouWant.contains($0["id"].intValue)}
for task in tasksYouWant {
if let ratings = task["safety_ratings"].array{
for submission in ratings {
if let score = submission["score"].string {
let scoreResult = Int(score)
self.ratingArray.append(scoreResult!)
}
if let ratingId = submission["safety_rating_id"].int {
self.ratingArray.append(ratingId)
}
if let submissionId = submission["submission_id"].int {
self.ratingArray.append(submissionId)
}
}
}
}
if let tasks = subJson["tasks"].array{
Tasks.sharedInstance.datas = tasks
}
}
print(self.ratingArray)
onCompletion()
}
case .Failure(let error):
print("Request failed with error: \(error)")
onError?(error)
}
}
}
When I print taskYouWant
I get an empty array..
This is the JSON response from my GET https://codeshare.io/UqJMV
What I am looking for is the score
, safety_rating_id
where my submission_id
is of x value. For example, to pull the array where submission_id
is 27.