如何识别JSON响应中的特定值 [英] How to identify specific values in JSON response

查看:84
本文介绍了如何识别JSON响应中的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取特定的JSON响应。例如,仅获取ID为25和26的数组中的结果(即名称和分数)。swift中是否有一个 WHERE 子句?如果有帮助,我正在使用Alamofire和SwiftyJSON。

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.

现在,我的代码只提取所有值,但我只想获取ID为25和26的结果。

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?

我的代码:

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

更新

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

当我打印 taskYouWant 时,我得到一个空数组。

When I print taskYouWant I get an empty array..

这是来自我的GET https://codeshare.io/UqJMV

This is the JSON response from my GET https://codeshare.io/UqJMV

我要寻找的是分数 safety_rating_id ,其中我的 submission_id 是x值。例如,拉出 submission_id 为27的数组。

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.

推荐答案

如何将subJson [ tasks]转换为数组然后对其进行过滤:

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
}

希望有帮助!

这篇关于如何识别JSON响应中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆