【问题标题】:How to parse json data with Alamofire?如何使用 Alamofire 解析 json 数据?
【发布时间】:2018-07-05 08:54:11
【问题描述】:

我正在制作一个类似这个视频的应用程序:https://www.youtube.com/watch?v=mzyFQ55M7eo

但我不希望 textview 显示 json 数据,我只希望它显示文本“Hello(用户名)”意思是 Hello(subject_id),但我不知道如何像视频一样使用 alamofire 解析 json。我正在研究这个,但没有运气。有人可以解释怎么做吗?提前感谢,对不起我的英语不好。

【问题讨论】:

  • 关于这个问题有很多答案......尝试使用它们,如果遇到问题,请在这里提问

标签: json swift alamofire kairos-api


【解决方案1】:

写一个函数来获取json数据。如果它的 Post 方法

let todosEndpoint: String = Your Url
            let  paramss: Parameters = ["user_id": self.userId]
            Alamofire.request(
                todosEndpoint,
                method: .post,
                parameters: paramss,
                headers: ["Content-Type": "application/x-www-form-urlencoded"])
                .responseJSON { response in
                    switch response.result {
                    case .success:
                            //if json data is a array
                            let json = JSON(response.result.value as Any)
                            if let codes = json.array {
                                for eachCode in codes {
                                    let userName = eachCode["username"].stringValue
                                     print("userName is\(userName)")

                                }
                            }

                            break

                        case .failure:
                            break
                        }
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-12
    • 2020-08-11
    相关资源
    最近更新 更多