2015-10-27 56 views
0

我想用Alamofire調用一個JSON數組內的對象。 我可以讓Alamofire解析我的文件,但是,我找不到獲取特定參數的方法。SWIFT 2 - alamofire - 解析JSON參數

如果我使用:

Alamofire.request(.GET, "https://example.com//users_result.json") 
      .responseJSON { response in 

       print(response.result) // result of response serialization 

       if let JSON = response.result.value { 
        print(JSON.count) 
        print("JSON: \(JSON[1])") 
       } 
     } 

代碼打印出:

> SUCCESS 4 JSON: { 
>  Age = 22; 
>  Email = "[email protected]"; 
>  Location = "New York"; 
>  MyText = "Love Music"; 
>  Name = Annah; 
>  Password = 123456; 
>  REF = 003; 
>  "Reg_Date" = "2015-07-30"; 
>  Sex = Female; 
>  Surname = Swift; } 

現在,我怎麼能調用參數 '名'?

print("JSON: \(JSON[1][4])") // nil ?? 
+0

的可能的複製[如何解析來自斯威夫特Alamofire API JSON響應?](http://stackoverflow.com/questions/26114831/how-to-parse-json-response-from- alamofire-api-in-swift)(這是一個,也可以是數千個之一) – Moritz

+0

還有:[Swift文檔,基礎知識:字典](https://developer.apple.com/library/prerelease/ios/)文檔/ Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#// apple_ref/doc/uid/TP40014097-CH8-ID105) – Moritz

回答

0
print("Response data: \(JSON[1]["Name"] as! String)") 
+0

你必須避開引號,不是嗎?或者分開陳述。 – Rob

+2

@Rob取決於:自Xcode 7.1 beta 3以來,我們不必再逃脫。 – Moritz

+0

非常好。我不知道。 – Rob