【问题标题】:Accessing JSON data using SwiftyJSON and Alamofire使用 SwiftyJSON 和 Alamofire 访问 JSON 数据
【发布时间】:2015-08-31 18:15:07
【问题描述】:

我创建了一个函数,该函数使用 Alamofire 通过 POST 请求成功返回 JSON 对象,并且可以使用正确的数据打印该对象。但是,如果我想访问 JSON 中的某个数据点,它要么打印为 null,要么说我以错误的方式访问它。这是我的代码结构,我想知道如何正确编写 print(post.description) 部分

func parseJSON(id:String, found: Bool) -> Dictionary { var dsProperties = 字符串:JSON

Alamofire.request(.POST, urlPath, parameters: parameters)
  .responseJSON { (request, response, result) in
    if let anError = result.error {

      print("error calling POST on /posts")
      print(anError)
    }

    if let result: AnyObject = result.value {
      let post: JSON = JSON(result)
      for (index: String, subJSON: JSON) in post {
        print(post.description) // how can i subscript this
      }
    }
}

【问题讨论】:

  • description 一个字符串吗?

标签: swift swift2 alamofire swifty-json


【解决方案1】:

查看SwiftyJson 中的subscription 部分:

Alamofire.request(.POST, urlPath, parameters: parameters)
  .responseJSON { (request, response, result) in
if let anError = result.error {

  print("error calling POST on /posts")
  print(anError)
}

if let result: AnyObject = result.value {
  let post: JSON = JSON(result)
  for (index: String, subJSON: JSON) in post {
    print(post["description"].stringValue)
  }
}

}

如果不是字符串,请切换stringValue为适当的数据类型

【讨论】:

    猜你喜欢
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多