2017-01-10 14 views
1

私はSwift 3で新しく、Jsonが返信してリクエストを送信するのに問題があります。私はパラメータのユーザー名とパスワードでサーバーにポストリクエストを送信しようとしていて、情報の付いたJsonとの応答を得ていますが、データを返すことができませんでした。Json Parsing Swift 3 Alamofire

出力:Android上で

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

要求は次のようになります。

  "{\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" + 
      "}"; 

これは私が要求に送るべきです:

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

これは私のコードです:

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