2017-02-10 98 views
2

這裏我想通過url解析JSON。這是在url上可用的實際JSON數據。所以我需要解析它並使用Alamofire在我的應用程序中閱讀。但我無法做到。如何使用SWIFT3解析此JSON

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. 
       } 
      } 
     } 

請幫幫我。我是新手。

+1

爲什麼有兩個'if let arr'?不要使用'NSDictionary',使用Swift Types。它通過了第一次測試嗎?如果是,如果let main = arr [「main」]爲? [[String:AnyObject]]'(字典數組)應該是下一個邏輯步驟。然後'讓val1 = main [0] [「USDARS」]爲? String'? – Larme

+0

你好,它不起作用 – aadityaa

+0

什麼是行不通的?你是一個潔具,你正在做多個「如果」測試,但是他們中的任何一個都可能失敗?哪一個確切(你可以添加一個「其他」案例)?你甚至通過了第一個? – Larme

回答

1
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) 
    } 
} 

要打印一個特定的值,你可以做到這一點有兩種方式:

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) 
    } 
} 

創建的所有值的字典:

每次搜索的東西的時間建立一個新的Alamofire請求然後打印搜索到的:

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

+0

您好,如果我想按日期列印例如:13-02-2017 USDRS值 – aadityaa

+0

如何使用貨幣值打印特定日期。 – aadityaa

+0

這裏它打印所有值,由於f​​or循環 – aadityaa

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