【问题标题】:How do I access a nested JSON value using Alamofire and SwiftyJSON?如何使用 Alamofire 和 SwiftyJSON 访问嵌套的 JSON 值?
【发布时间】:2016-02-09 09:06:17
【问题描述】:

我正在尝试使用 swiftyJSON 和 Alamofire 访问嵌套的 JSON 结果。我的打印值为零,我相信我没有正确执行此操作。我的参数应该是什么?我正在尝试获取位于 http://quotes.rest/qod.json

的报价值
func getAPI() {
    Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
        .responseJSON { response in
            if let JSON = response.result.value {
                print(JSON["quote"])

            }
    }
}

【问题讨论】:

    标签: json swift alamofire swifty-json


    【解决方案1】:

    在您的 JSON 中,quotes 是一个数组,因此如果您想访问第一个对象的 quote,您应该通过访问第一个对象来实现:

     func getAPI() {
            Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
                .responseJSON { response in
                    if let jsonValue = response.result.value {
                        let json = JSON(jsonValue)
                        if let quote = json["contents"]["quotes"][0]["quote"].string{
                         print(quote)
                        }
                    }
            }
        }
    

    【讨论】:

    • 太棒了!谢谢你的解释,它很好用,我理解!
    【解决方案2】:

    如果 json 的语法不正确,因为它已经完全打印出来,你应该注意到哪里出了问题。

    func getAPI() {
    Alamofire.request(.GET, "http://quotes.rest/qod.json", parameters: ["contents": "quotes"])
        // JSON response
            .responseJSON { response in switch response.result {
            case .Failure(let error):
                // got an error in getting the data, need to handle it
                print("error calling GET, json response type :")
                // print alamofire error code
                let statusCode = error.code
                print("error code json : \(statusCode)")
                // print json response from server
                if let data = response.data {
                    print("Response data: \(NSString(data: data, encoding: NSUTF8StringEncoding)!)")
                }
                // print http status code plus error string
                print(NSHTTPURLResponse.localizedStringForStatusCode(statusCode))
                if let httpResponse : NSHTTPURLResponse = response.response {
                    print("HTTP Response statusCode: \(httpResponse.statusCode)")
                }
            case .Success( _):
                let statusCode = (response.response?.statusCode)!
                print("status code json : \(statusCode)")
                print("there is a response json")
                //print(value)
                // parse the result as JSON, since that's what the API provides and save datas as new user in coreData
                guard let data = response.data else {
                    print("Error parsing response data")
                    return
                }
                let json = JSON(data: data)
                // access first element of the array
                if let postContent = json["contents"]["quotes"][0]["quote"].string{
                 // deal with json
                }
           }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-13
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 1970-01-01
      • 2014-12-27
      • 2015-04-24
      • 2020-08-28
      相关资源
      最近更新 更多