Ios 施工过于复杂,无法在合理时间内解决
我对Swift 3有问题,我试图向服务器发送请求并获取JSON,但我得到: 施工过于复杂,无法在合理的时间内解决 我试过各种方法,但都不管用Ios 施工过于复杂,无法在合理时间内解决,ios,json,xcode,swift3,Ios,Json,Xcode,Swift3,我对Swift 3有问题,我试图向服务器发送请求并获取JSON,但我得到: 施工过于复杂,无法在合理的时间内解决 我试过各种方法,但都不管用 var userName = "root" var password = "admin01" //var LOGIN_TOKEN = 0000000000000000 let parameters = [ "{\n", " \"jsonrpc\": \"2.0\",\n", " \"id\": \"1\",\n",
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,但在那之后,我在请求时遇到了问题,我声明了我的参数。他们说:使用未解析的标识符
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)")
更新:
根据Alamofire的文档(您可以看到),您不需要将参数字典转换为JSON。
比如说,
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
旧的:
实际上,您应该为参数使用术语
因此,您不应该像以前那样声明参数,而是应该这样做:
let parameters = ["jsonrpc" : 2.0,
"id": 1,
"method": "call",
"params": [ "00000000000000",
"session",
"login",
[ "username": userName,
"password": password]],
]
无论如何,不要手动创建JSON字符串。创建数组和字典,然后将它们转换为JSON。现在我得到一个错误:调用中有额外的参数'method'。你能帮我吗?这只是Swift 2中的一个例子。重要的是想法。你可以自己找到其他的例子,有很多。我创建了dic并转换为JSon,但我有另一个问题,请检查我的问题,我更新了它。我尝试过这种方式,但现在我的请求有问题,我更新了我的问题,请检查,并帮助我