首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Swift中解析来自Alamofire API的JSON响应?

如何在Swift中解析来自Alamofire API的JSON响应?
EN

Stack Overflow用户
提问于 2014-09-30 15:12:13
回答 12查看 206.5K关注 0票数 132

下面是我写的代码,我也得到了JSON的响应,但是JSON的类型是"AnyObject“,我不能把它转换成数组,这样我就可以使用它了。

代码语言:javascript
运行
复制
Alamofire.request(.POST, "MY URL", parameters:parameters, encoding: .JSON) .responseJSON
{
    (request, response, JSON, error) in

    println(JSON?)
}
EN

回答 12

Stack Overflow用户

发布于 2015-10-09 02:03:48

Swift 2.0 Alamofire 3.0的答案实际上应该更像这样:

代码语言:javascript
运行
复制
Alamofire.request(.POST, url, parameters: parameters, encoding:.JSON).responseJSON
{ response in switch response.result {
                case .Success(let JSON):
                    print("Success with JSON: \(JSON)")

                    let response = JSON as! NSDictionary

                    //example if there is an id
                    let userId = response.objectForKey("id")!

                case .Failure(let error):
                    print("Request failed with error: \(error)")
                }
    }

https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%203.0%20Migration%20Guide.md

Alamofire 4.0和Swift 3.0的更新:

代码语言:javascript
运行
复制
Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default)
            .responseJSON { response in
                print(response)
//to get status code
                if let status = response.response?.statusCode {
                    switch(status){
                    case 201:
                        print("example success")
                    default:
                        print("error with response status: \(status)")
                    }
                }
//to get JSON return value
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                print(JSON)
            }

        }
票数 163
EN

Stack Overflow用户

发布于 2015-02-20 13:59:16

与上面提到的一样,您可以使用SwiftyJSON库并获得您的值,就像我下面所做的那样

代码语言:javascript
运行
复制
Alamofire.request(.POST, "MY URL", parameters:parameters, encoding: .JSON) .responseJSON
{
    (request, response, data, error) in

var json = JSON(data: data!)

       println(json)   
       println(json["productList"][1])                 

}

我的json产品列表从脚本返回

代码语言:javascript
运行
复制
{ "productList" :[

{"productName" : "PIZZA","id" : "1","productRate" : "120.00","productDescription" : "PIZZA AT 120Rs","productImage" : "uploads\/pizza.jpeg"},

{"productName" : "BURGER","id" : "2","productRate" : "100.00","productDescription" : "BURGER AT Rs 100","productImage" : "uploads/Burgers.jpg"}    
  ]
}

输出:

代码语言:javascript
运行
复制
{
  "productName" : "BURGER",
  "id" : "2",
  "productRate" : "100.00",
  "productDescription" : "BURGER AT Rs 100",
  "productImage" : "uploads/Burgers.jpg"
}
票数 31
EN

Stack Overflow用户

发布于 2017-06-09 11:34:32

Swift 3、Alamofire 4.4和SwiftyJSON:

代码语言:javascript
运行
复制
Alamofire.request(url, method: .get)
  .responseJSON { response in
      if response.data != nil {
        let json = JSON(data: response.data!)
        let name = json["people"][0]["name"].string
        if name != nil {
          print(name!)
        }
      }
  }

它将解析这个JSON输入:

代码语言:javascript
运行
复制
{
  people: [
    { name: 'John' },
    { name: 'Dave' }
  ]
}
票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26114831

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档