2016-05-05 114 views
-1

我正在使用Xcode在Swift中開發iOS應用程序。如何從此HTTP響應中獲取Json對象的值。我需要內部消息和狀態的值。從HTTP響應中獲取Json對象的字段值

{"message":"success","statusCode":200,"data":{"message":"already logged in","status":0}} 

預先感謝您:)

+0

你可以試試[這](http://stackoverflow.com/a/30480777/4519092) – GeekRiky

+0

請檢查鏈接。希望能爲你工作。 http://stackoverflow.com/a/30480777/1616632 –

回答

0

找那本字典和

let objDict = yourResponseObject.valueForKey("Data") as! NSDictionary 

if objDict.valueforkey("message").isequaltoString("already logged in") 

{ 

User logged already 

} 

else 

{ 

not logged 

} 
0

首先,我建議對REST調用(HTTP),它被稱爲Alamofire最好的第三方API。

如果使用下面Alamofire例如:

Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"]) 
    .responseJSON { response in 
     print(response.request) // original URL request 
     print(response.response) // URL response 
     print(response.data)  // server data 
     print(response.result) // result of response serialization 

     // Parsing the JSON 
     if let JSON = response.result.value { 
      print("JSON: \(JSON)") 

      // The actual cast to Dictionary 
      let jsonDictionary = JSON as! Dictionary<String, AnyObject> 
     } 
    } 

基本上jsonDictionary將是您的JSON字典,鍵是String和值AnyObject型的(所以你可以投你需要什麼都)和現在你可以做以下電話:

let message = json["message"] as? String