【问题标题】:Parse json object in dictionary using Alamofire [closed]使用 Alamofire 解析字典中的 json 对象 [关闭]
【发布时间】:2017-06-20 04:44:12
【问题描述】:

您好,这是我的 json 响应,我如何将“数据”参数值存储在数组中。我正在使用 Alamofire 4 和 swift 3。我想要这个数组格式,用于在 tableview 中加载内容。

 Optional({
"current_page" = 2;
data =     (
            {
        "address_location" = "place   of course";
        "category_id" = 1;
        "course_id" = 122;
        "course_title" = title;
        description = "dessription about the course";
        favorited = 0;
        "from_date" = "2017-02-16";
        image = "<path>";
        lattitude = "0.876423656";
        longitude = "6.86885314";
        price = 200;
        "subcategory_id" = 10;
        thumb = "<path>";
        "to_date" = "2017-02-16";
        "user_id" = 32332;
    },
            {
        "address_location" = "place   of course";
        "category_id" = 1;
        "course_id" = 123;
        "course_title" = title;
        description = "dessription about the course";
        favorited = 0;
        "from_date" = "2017-02-16";
        image = "<path>";
        lattitude = "0.876423656";
        longitude = "6.86885314";
        price = 200;
        "subcategory_id" = 10;
        thumb = "<path>";
        "to_date" = "2017-02-16";
        "user_id" = 1;
    }
);
message = Success;
"next_page" = 3;
"previous_page" = 1;
"status_code" = 200;

})

API 调用操作代码

    Alamofire.request(urlString, method: .get, parameters: ["":""], encoding: URLEncoding.default, headers: nil).responseJSON { (response:DataResponse<Any>) in

        switch(response.result) {
        case .success(_):
            if response.result.value != nil{
                print(response.result.value as Any)

                //

                self.parseData(JSONData: response.data!)

                if let jsonDict = response.result.value as? [String:Any], 
                    let dataArray = jsonDict["data"] as? [[String:Any]] {
                    self.dictionary_Course = dataArray  as? [[String:Any]] 
                    let nameArray = dataArray.flatMap { $0["address_location"] as? String }
                    print(nameArray)
                }                    
                print(self.dictionary_Course)


            }
            else
            {
            }
            break

        case .failure(_):
            print(response.result.error as Any)
            break

        }
    }

【问题讨论】:

  • 它是可选值。你能添加代码吗?

标签: ios iphone swift alamofire


【解决方案1】:

试试这个:

Alamofire.request("url", method: .post, parameters: ["":""]).validate().responseJSON { response in
            switch response.result {
            case .success:
                if let result = response.result.value {
                    let responseDict = result as! [String : Any]
                    let data = responseDict["data"] as! [Any]
                    print(data)
                }
            case .failure(let error):
                print(error)
            }
        }

【讨论】:

  • 它可以工作,但是我如何将数据分配到数组中,当我尝试它时显​​示错误“无法将类型 '__NSArrayI' () 的值转换为 'NSDictionary' ()。”
  • 你在哪一行出错了?
  • 试试我更新的答案。数据是你的数组。
  • @KKRocks :- 我也面临同样的问题,我是 swift 语言的新手。使用您的答案,我可以将数据存储在数组中 var testArray = [Any]() , testArray = data as! [Any] 我想访问 testArray[0]["address_location"]。这该怎么做 ?我收到错误:类型“任何”没有下标成员此错误
  • 试试这个:testArray = responseDict["data"] as! [任何]
【解决方案2】:

Swift 3.0

试试下面的代码,希望对你有帮助。

if let result = response.result.value {
                    if let responseDict = result as? NSDictionary {
                        if let data = responseDict.value(forKey: "data") as?
                            [NSDictionary] {

                        }
                        print(data)
                    }

                }

【讨论】:

  • 我想将对象分配到数组或字典中。我如何分配 var dictionary_Course = [String: String]()
  • 是的,但是您可以看到数据已经在 NSDictionary 的数组中。
猜你喜欢
  • 1970-01-01
  • 2017-04-01
  • 1970-01-01
  • 2013-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多