0
要解決我的問題,斯威夫特3,我試圖發送一個請求到服務器,並獲得JSON,但我得到:施工太複雜在合理的時間
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)")
}
}
現在我tryed來創造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