2016-09-21 12 views
-1

JSON Respose是SWIFTY JSON工作不更新到Xcode的8

{ 
    code = 1; 
    document =  (
       { 
      Address1 = "<null>"; 
      Address2 = "<null>"; 
      City = "<null>"; 
      CompanyID = 1; 
      CompanyName = Innohabit; 
      ContactNumber = "<null>"; 
      Designation = "<null>"; 
      DesignationID = 36; 
      DesignationName = dev; 
      Email = "[email protected]"; 
      FCMRegistrationToken = ""; 
      GCMRegistrationToken = ""; 
      Gender = Male; 
      ID = 9; 
      IsActive = 1; 
      IsFirstLogin = 1; 
      LastLogin = "2016-09-21 05:45:33"; 
      Name = "Uday Mishra"; 
      Password = ""; 
      ProfilePic = ""; 
      RegisterationDate = "2016-03-04 07:42:42"; 
      UserTwitter = "<null>"; 
      Username = "[email protected]"; 
     } 
    ); 
    message = "Record found"; 
    status = success; 
} 

,並試圖轉換使用Swifty Json喜歡這個

  case .success: 
      print(response) 

      let jsonResponse = JSON(response.data) 
      print("JSON") 
      let jsonData = jsonResponse["document"] 
      print(jsonResponse) 
      print(jsonData[0]) 

的respose但我沒有得到期望的結果,輸出是

{ 
    code = 1; 
    document =  (
       { 
      Address1 = "<null>"; 
      Address2 = "<null>"; 
      City = "<null>"; 
      CompanyID = 1; 
      CompanyName = Innohabit; 
      ContactNumber = "<null>"; 
      Designation = "<null>"; 
      DesignationID = 36; 
      DesignationName = dev; 
      Email = "[email protected]"; 
      FCMRegistrationToken = ""; 
      GCMRegistrationToken = ""; 
      Gender = Male; 
      ID = 9; 
      IsActive = 1; 
      IsFirstLogin = 1; 
      LastLogin = "2016-09-21 05:45:33"; 
      Name = "Uday Mishra"; 
      Password = ""; 
      ProfilePic = ""; 
      RegisterationDate = "2016-03-04 07:42:42"; 
      UserTwitter = "<null>"; 
      Username = "[email protected]"; 
     } 
    ); 
    message = "Record found"; 
    status = success; 
} 
JSON 
unknown 
null 

正如你可以看到響應正在打印,但th Ë打印(jsonResponse)打印(jsonData [0]) 是印刷未知誰能幫助我,我如何從DataRespose

連載中的數據正確地爲JSON格式請提出替代方法或如果我在做任何錯誤,請幫助我。

+1

莢 'SwiftyJSON',:git的=> 'https://github.com/appsailor/SwiftyJSON.git':使用同樣的事情在我的POD文件 –

+0

我還在不要求幫助。 –

+0

支=> 'swift3' –

回答

0

我試過了,它對我來說工作正常。

Alamofire.request(url, method: .post,parameters: parameters).validate().responseJSON { response in 
       switch response.result { 
       case .success(let data): 
        //print(response) 

        let jsonResponse = JSON(data) 
        print("JSON") 
        let jsonData = jsonResponse["document"][0] 
        print("Email") 
        print(jsonData["Email"]) 



       case .failure(let error): 
        print(error) 
       } 
      } 
+0

我的朋友「Alamofire」已經在工作。你需要SwiftyJson的解決方案。 –

0

嘗試使用波紋管代碼:

if let jsonResponse: AnyObject = JSON(response.data){ 
    let jsonData = jsonResponse["document"] 
    print(jsonResponse) 
    print(jsonData[0]) 
} 

編輯:

我對this

目標吊艙的平臺閱讀以下聲明(的iOS 7.1)不兼容與SwiftyJSON(2.1.3),其中最低要求OS X 10.9 - iOS 8.0。 編輯: 我用Xcode 6.3分支中的SwiftyJson.swift替換了它,現在它工作正常,沒有錯誤。 :-)

所以請儘量按照這一步。

+0

我越來越錯誤: - 無法從'JSON'向下轉換爲更多可選類型'[String:AnyObject]!' –

+0

我已經更新了現在檢查的答案。 –