2017-05-31 46 views
0

我連接到本地主機上的服務器以獲取一些數據。數據回到了這個請求是JSON,在郵差的JSON顯示正確,但是當我與iOS玩,Alamofire返回我的錯誤:
Alamofire垃圾在解析JSON結束時出錯

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}))


的JSON的問題是:

{ 
    "name": "TestName", 
    "surname": "TestSurname" 
} 

,我不明白的事情是,如果我強迫我的服務器以字符串形式返回JSON所以像

"{"name": "TestName after update","surname": "TestSurname"}" 

Alamofire不會抱怨並正確解析它。那個怎麼樣?我認爲指定參數responseJSON它會反過來工作。

Alamofire.request("http://192.168.1.4:8080/user/abcdf").validate().responseJSON { response in 
      switch response.result { 
      case .success: 
       // DO stuff 

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

我剛剛查了JSON通過創建一個JSON的服務器,它工作得很好 –

+0

但我添加了JSON爲對象,像:[ { 名稱:「測試名」, 姓:「TestSurname」 } ] –

+0

所以可能它的生成JSON的方式,將深入到它 – r4id4

回答

0

我認爲你需要獲取數據,所以你應該有它這樣寫的我不知道,雖然

Alamofire.request("http://192.168.1.4:8080/user/abcdf",method:.get).responseJSON { 
     response in 
     if response.result.isSuccess { 
      //do stuff 

     } 
     else { 

      // do other stuff 
     } 
    }