施工太复杂,无法在合理的时间内解决

时间:2017-01-09 18:47:30

标签: 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",
    "    \"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)")

1 个答案:

答案 0 :(得分:0)

更新:

根据Alamofire的文档(您可以看到here),您无需将参数Dictionary转换为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

OLD:

您应该实际使用Dictionay作为参数。

因此,您应该这样做:

,而不是像您一样声明参数
let parameters = ["jsonrpc" : 2.0,
              "id": 1,
              "method": "call",
              "params": [ "00000000000000",
                          "session",
                          "login",
                          [ "username": userName,
                            "password": password]],
            ]