如何使用SWIFT3

时间:2017-02-10 13:25:32

标签: ios json swift swift3

这里我想通过url解析JSON。这是url上可用的实际JSON数据。所以我需要解析它并使用Alamofire在我的应用程序中阅读。但我无法做到。

我的网址中的JSON数据。

[{"date":"2017-01-15","USDARS":"15.839023","USDCLP":"659.200012","USDVEF":"9.974801","USDCOP":"2943","USDPEN":"3.359497","USDBOB":"6.859692","USDPYG":"5780.799805","USDUYU":"28.660097","USDGTQ":"7.533499","USDCUP":"0.999845","USDCRC":"547.599976","USDHTG":"64.129997","USDJMD":"128.309998","USDDOP":"46.230097","USDETB":"22.459999","USDNGN":"314.999972","USDEGP":"18.900947","USDCDF":"1227.999527","USDTZS":"2216.999869","USDKES":"103.650002","USDSDG":"6.4077"},

    {"date":"2017-01-16","USDARS":"15.849011","USDCLP":"662.099976","USDVEF":"9.974797","USDCOP":"2936.800049","USDPEN":"3.367975","USDBOB":"6.860284","USDPYG":"5733.700195","USDUYU":"28.749899","USDGTQ":"7.545501","USDCUP":"0.999646","USDCRC":"547.099976","USDHTG":"64.129997","USDJMD":"128.179993","USDDOP":"46.230123","USDETB":"22.39805","USDNGN":"313.999947","USDEGP":"18.898647","USDCDF":"1225.999725","USDTZS":"2221.999959","USDKES":"103.650002","USDSDG":"6.407702"},

    {"date":"2017-01-17","USDARS":"15.882954","USDCLP":"656.299988","USDVEF":"9.974798","USDCOP":"2914","USDPEN":"3.3392","USDBOB":"6.860283","USDPYG":"5771.000017","USDUYU":"28.680621","USDGTQ":"7.533498","USDCUP":"0.999979","USDCRC":"547.080017","USDHTG":"64.120003","USDJMD":"128.580002","USDDOP":"46.200001","USDETB":"22.397891","USDNGN":"314.999989","USDEGP":"18.649708","USDCDF":"1225.000306","USDTZS":"2227.000108","USDKES":"103.750025","USDSDG":"6.407696"}]

如何在swift 3.0中使用Alamofire阅读它

以下是我尝试通过url解析JSON数据以上的内容。

Alamofire.request("myurl") .responseJSON { response in

            print("In Alamofire")
            if let arr = response.result.value as? [String:AnyObject]
            {

                if let arr = response.result.value as? [NSDictionary]
                {
                    let val1 = (arr["main"]["USDARS"] as? String)
                    print(val1)
                    //It does not print any thing.
                }
            }
        }

请帮帮我。我是新手。

2 个答案:

答案 0 :(得分:2)

我建议使用EVReflection,您必须创建2个模型,然后才能轻松管理结果。

型号:

class RootElementModel : EVObject{
    var main : [MainElement];
}

class MainElementModel : EVObject{
    var date : String = "";
    var USDARS : CGFloat = 0;
    var USDCLP : CGFloat = 0;
    var USDSDG : CGFloat = 0;
}

然后您可以使用如下:

Alamofire.request("url").responseString{
response in
    let resString = response.result.value
    let items = RootElementModel(json : resString).main
    print("first item USDARS = \(items[0].USDARS)")
}

有关更多用法示例和安装教程,请阅读Official Documentation

答案 1 :(得分:0)

Alamofire.request(url).responseJSON(completionHandler: {
        response in
        self.parseData(JSONData: response.data!)
})

typealias JSONStandard = [String : AnyObject]

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let items = readableJSON["main"] as? [JSONStandard] {
            for i in 0..<items.count  {
                let item = items[i]
                    if let USDARS = item["USDARS"]
                        print(USDARS)
            }
        }
    } catch {
        print(error)
    }
}

要打印特定值,您可以通过两种方式执行此操作:

每次搜索时都会发出新的Alamofire请求:

let whatToSearchFor: String = "13-02-2017"
Alamofire.request(url).responseJSON(completionHandler: {
        response in
        self.parseData(JSONData: response.data!)
})

typealias JSONStandard = [String : AnyObject]

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let items = readableJSON["main"] as? [JSONStandard] {
            for i in 0..<items.count  {
                let item = items[i]
                    guard item["date"] != nil else { print("nil"); return}
                    guard item["USDARS"] != nil else { print("nil"); return}
                    if item["date"] == whatToSearchFor {
                        print(item["USDARS"])
                    }
            }
        }
    } catch {
        print(error)
    }
}

创建一个包含所有值的字典,然后打印搜索到的字典:

let dict = [String : String]

Alamofire.request(url).responseJSON(completionHandler: {
        response in
        self.parseData(JSONData: response.data!)
})

typealias JSONStandard = [String : AnyObject]

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let items = readableJSON["main"] as? [JSONStandard] {
            for i in 0..<items.count  {
                let item = items[i]
                guard item["date"] != nil else { print("nil"); return}
                guard item["USDARS"] != nil else { print("nil"); return}
                let date = item["date"]
                let USDARS = item["USDARS"]
                dict[date] = USDARS
            }
        }
    } catch {
        print(error)
    }
}
let whatToSearchFor: String = "13-02-2017"
print(dict[whatToSearchFor])

在JSON中没有“main”后进行编辑:

let whatToSearchFor: String = "13-02-2017"
Alamofire.request(url).responseJSON(completionHandler: {
        response in
        self.parseData(JSONData: response.data!)
})

typealias JSONStandard = [String : AnyObject]

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let items = readableJSON {
            for i in 0..<items.count  {
                let item = items[i]
                    guard item["date"] != nil else { print("nil"); return}
                    guard item["USDARS"] != nil else { print("nil"); return}
                    if item["date"] == whatToSearchFor {
                        print(item["USDARS"])
                    }
            }
        }
    } catch {
        print(error)
    }
}

或者:

let dict = [String : String]

Alamofire.request(url).responseJSON(completionHandler: {
        response in
        self.parseData(JSONData: response.data!)
})

typealias JSONStandard = [String : AnyObject]

func parseData(JSONData : Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let items = readableJSON {
            for i in 0..<items.count  {
                let item = items[i]
                guard item["date"] != nil else { print("nil"); return}
                guard item["USDARS"] != nil else { print("nil"); return}
                let date = item["date"]
                let USDARS = item["USDARS"]
                dict[date] = USDARS
            }
        }
    } catch {
        print(error)
    }
}
let whatToSearchFor: String = "13-02-2017"
print(dict[whatToSearchFor])

基本上main消失,as? [JSONStandard]也消失,因为readableJSON定义为JSONStandard