妥当な時間で解決されるにはあまりに複雑で、私は、サーバーに要求を送信し、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)")
とにかく手動でJSON文字列を作成しないでください。配列と辞書を作成し、JSONに変換します。 http://stackoverflow.com/a/31263337/2227743 – Moritz
これでエラーが発生しました。余分な引数 'メソッド'が呼び出されました。あなたはそれを手伝ってもらえますか? –
これはちょうどSwift 2に起こった例です。重要なのはアイデアです。あなたは自分で他の例を見つけることができます。多くの例があります。 – Moritz