How to identify specific values in JSON response

删除回忆录丶 提交于 2019-12-12 02:36:25

问题


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.


回答1:


how about casting subJson["tasks"] to an array and then filtering it:

let IDsYouWant: [Int] = [25, 26]
let tasksYouWant = subJson["tasks"].arrayValue.filter {IDsYouWant.contains($0["id"].intValue)}

for task in tasksYouWant {
    //process task 25 and 26
}

hope that helps!



来源:https://stackoverflow.com/questions/37540689/how-to-identify-specific-values-in-json-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!