2017-01-10 3 views
1

저는 Swift 3에서 새로 왔으며 Json이 요청을 보내면 문제가 생겼습니다. 나는 사용자 이름과 암호 매개 변수를 사용하여 서버에 게시물 요청을 보내려고하고 정보가있는 Json과 응답을 얻으려고 시도하지만 반환 할 데이터를 가져올 수 없었습니다.Json Pearing Swift 3 Alamofire

출력 :

: 이것은 내 코드입니다

"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\" } ] }" 

:

  "{\n" + 
      " \"jsonrpc\": \"2.0\",\n" + 
      " \"id\": \"1\",\n" + 
      " \"method\": \"call\",\n" + 
      " \"params\": [\n" + 
      "  \"" + LOGIN_TOKEN + "\",\n" + 
      "  \"session\",\n" + 
      "  \"login\",\n" + 
      "  {\n" + 
      "   \"username\": \"" + userName + "\",\n" + 
      "   \"password\": \"" + password + "\"\n" + 
      "  }\n" + 
      " ]\n" + 
      "}"; 

이 내가 요청에 보내야하는 것입니다 : 안드로이드에

error: NSURLErrorDomain: -1003 status code: 200, headers Connection = close; "Content-Type" = "application/json"; "Transfer-Encoding" = Identity

요청과 같은

override func viewDidLoad() { 
    var userName = "root" 
    var password = "admin01" 
    //var LOGIN_TOKEN = 0000000000000000 

    let jsonObject: [String: Any] = 
     ["jsonrpc" : 2.0, 
     "id": 1, 
     "method": "call", 
     "params": [ "00000000000000", 
        "session", 
        "login", 
        [ "username": userName, 
         "password": password]], 
     ] 

    do { 
     let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) 
     // here "jsonData" is the dictionary encoded in JSON data 

     let decoded = try JSONSerialization.jsonObject(with: jsonData, options: []) 
     // here "decoded" is of type `Any`, decoded from JSON data 

     // you can now cast it with the right type 
     if let dictFromJSON = decoded as? [String:String] { 
      // use dictFromJSON 
     } 
    } catch { 
     print(error.localizedDescription) 
    } 

    Alamofire.request("http://192.168.1.1/ubus", method: .post, parameters: jsonObject, encoding: JSONEncoding.default) 
     .responseJSON { response in 
      print(response) 
      //to get status code 
      if let status = response.response?.statusCode { 
       switch(status){ 
       case 201: 
        print("example success") 
       default: 
        print("error with response status: \(status)") 
       } 
      } 
      //to get JSON return value 
      if let result = response.result.value { 
       let JSON = result as! NSDictionary 
       print(JSON) 
      } 

    } 

    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 
+0

SwiftyJSON을 사용해보세요. https://github.com/SwiftyJSON/SwiftyJSON – JuicyFruit

답변

1

JSON 개체가 유효하지 않은 것 같습니다.

정말 유용한 JSON 검사기가 있습니다.

[ 
    { 
    "jsonrpc": 2.0, 
    "id": 1, 
    "method": "call", 
    "params": ["00000000000000", "session", "login"], 
    "username": username, 
    "password": password 
    } 
] 

I : 마음에 오는 하나는 http://jsonlint.com

내가 당신 된 JSONObject가 유효한 JSON입니다 이런 식으로 뭔가를보고 싶어, 당신이 게시 코드로 해석 할 수있는 것과 생각입니다 usernamepassword 변수는 이미 할당 한 문자열이라고 가정합니다.

+0

몇 가지 오류가 있습니다. "jsonrpc"필요 후에; 모든 타입은 배열과 함께 사용할 수 없습니다. 나는 onAndroid 스튜디오와 그 작업을 완벽하게 요청했습니다. –

+0

@GediminasUrbonas - 이것이 JSON 코드이므로 'Swift' 객체로 만들려고합니다. 위의 코드로 JSON 파일을 작성한 다음 JSON을 Data로 변환하는 방법을 배워야한다. JSON 코드를'Swift Data '로 변환 할 수 없다면 어떻게 든 변환되어야합니다. – Pierce

관련 문제