2017-01-09 4 views
0

妥当な時間で解決されるにはあまりに複雑で、私は、サーバーに要求を送信し、JSONを取得しようとしていますが、私は得る:建設は、私はスウィフト3に問題がある

Construction was too complex to be solved in reasonable time.

私はあらゆる方法を試してみました、しかしそれは働かない。

var userName = "root" 
var password = "admin01" 
//var LOGIN_TOKEN = 0000000000000000 

let parameters = [ 
    "{\n", 
    " \"jsonrpc\": \"2.0\",\n", 
    " \"id\": \"1\",\n", 
    " \"method\": \"call\",\n", 
    " \"params\": [\n", 
    "  \"0000000000000000\",\n", 
    "  \"session\",\n", 
    "  \"login\",\n", 
    "  {\n", 
    "   \"username\": \"" + userName + "\",\n", 
    "   \"password\": \"" + password + "\"\n", 
    "  }\n", 
    " ]\n", 
    "}" 
] 

let joiner = "" 
let joinedStrings = parameters.joined(separator: joiner) 
print("joinedStrings: \(joinedStrings)") 

// All three of these calls are equivalent 
Alamofire.request("http://192.168.1.1", method: .post, parameters: parameters).responseJSON { response in 
    print("Request: \(response.request)") 
    print("Response: \(response.response)") 


    if let JSON = response.result.value { 
     print("JSON: \(JSON)") 
    } 
} 

は今、私はDICクリートとJSONに変換するためにしようと試みたが、その後、私は私のパラメータを宣言がリクエストに応じて問題を取得します。彼らは言う:未解決の識別子を使用dictFromJSON

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) 
    } 




    // All three of these calls are equivalent 
    Alamofire.request("http://192.168.1.1/ubus", method: .post, parameters: dictFromJSON).responseJSON { response in 
     print("Request: \(response.request)") 
     print("Response: \(response.response)") 
+2

とにかく手動でJSON文字列を作成しないでください。配列と辞書を作成し、JSONに変換します。 http://stackoverflow.com/a/31263337/2227743 – Moritz

+0

これでエラーが発生しました。余分な引数 'メソッド'が呼び出されました。あなたはそれを手伝ってもらえますか? –

+0

これはちょうどSwift 2に起こった例です。重要なのはアイデアです。あなたは自分で他の例を見つけることができます。多くの例があります。 – Moritz

答えて

0

更新日:

Alamofire、(あなたがhereを見ることができる)の文書によると、あなたはJSONにパラメータの辞書を変換する必要はありません。例えば

、OLD

let parameters: Parameters = [ 
    "foo": "bar", 
    "baz": ["a", 1], 
    "qux": [ 
     "x": 1, 
     "y": 2, 
     "z": 3 
    ] 
] 

// All three of these calls are equivalent 
Alamofire.request("https://httpbin.org/post", parameters: parameters) 
Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.default) 
Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.httpBody) 

// HTTP body: foo=bar&baz[]=a&baz[]=1&qux[x]=1&qux[y]=2&qux[z]=3 

あなたが実際にパラメータのDictionayを使用する必要があります。

そのため、代わりにあなたが行ったように、パラメータを宣言するのは、このように実行する必要があります。

let parameters = ["jsonrpc" : 2.0, 
       "id": 1, 
       "method": "call", 
       "params": [ "00000000000000", 
          "session", 
          "login", 
          [ "username": userName, 
          "password": password]], 
      ] 
+0

私はこの方法で試しましたが、今はリクエストで問題が発生しました。質問を更新して確認してください –

関連する問題