Json 解析 Swift 3 Alamofire

标签 json swift parsing alamofire

我是 Swift 3 的新手,我在获取 Json 返回然后发送请求时遇到了问题。我正在尝试使用参数用户名和密码向服务器发送发布请求,并使用 Json 获取包含信息的响应,但我无法获取要返回的数据。

输出:

error: NSURLErrorDomain: -1003 status code: 200, headers Connection = close; "Content-Type" = "application/json"; "Transfer-Encoding" = Identity

Android 上的请求看起来像:

            "{\n" +
            "    \"jsonrpc\": \"2.0\",\n" +
            "    \"id\": \"1\",\n" +
            "    \"method\": \"call\",\n" +
            "    \"params\": [\n" +
            "        \"" + LOGIN_TOKEN + "\",\n" +
            "        \"session\",\n" +
            "        \"login\",\n" +
            "        {\n" +
            "            \"username\": \"" + userName + "\",\n" +
            "            \"password\": \"" + password + "\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";

这是我应该发送给请求的内容:

"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\"  } ] }" 

这是我的代码:

   override func viewDidLoad() {
   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)
    }

    Alamofire.request("http://192.168.1.1/ubus", method: .post, parameters: jsonObject, encoding: JSONEncoding.default)
        .responseJSON { response in
            print(response)
            //to get status code
            if let status = response.response?.statusCode {
                switch(status){
                case 201:
                    print("example success")
                default:
                    print("error with response status: \(status)")
                }
            }
            //to get JSON return value
            if let result = response.result.value {
                let JSON = result as! NSDictionary
                print(JSON)
            }

    }

    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

最佳答案

您的 JSON 对象似乎无效

有非常有用的免费 JSON 验证器。我想到的是 http://jsonlint.com

我认为,根据您发布的代码我可以解释,您希望您的 jsonObject 看起来像这样,这是一个有效的 JSON:

[
    {
    "jsonrpc": 2.0,
    "id": 1,
    "method": "call",
    "params": ["00000000000000", "session", "login"],
    "username": username,
    "password": password
    }
]

我假设 usernamepassword 变量是您已经分配的字符串?

关于Json 解析 Swift 3 Alamofire,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41576229/

相关文章:

java - 无法将 JSON 发送到 Spring Boot 应用程序中的其余端点

swift - 带按钮和 UITableView 的快速下拉菜单

ios - Swift 和录音

ios - 停止 UIScrollView 中的对象移动

javascript - JS - 处理初始服务器响应以显示 JSON

javascript - 在 Firefox 和 Chrome 中使用 jQuery 获取 JSON 结果时出现问题(IE8 工作)

java - Jackson 错误地将嵌套对象的值反序列化到父对象中

java - 使用 Dom 解析器加载 XML 文件时出现问题

java - 如何从加载的 Java 代码创建 UML 图?

excel - 使用字符串操作从 Excel 解析文本文件