首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Json解析Swift 3 Alamofire

Json解析Swift 3 Alamofire
EN

Stack Overflow用户
提问于 2017-01-10 18:52:00
回答 1查看 473关注 0票数 1

我在Swift 3是新来的,我在让Json回来然后发送请求方面遇到了问题。我试图用参数用户名和密码向服务器发送一个post请求,并通过Json获得一个信息响应,但我没有能够获得返回的数据。

输出:

错误: NSURLErrorDomain:-1003状态代码: 200,标头连接=关闭;“内容-类型”= "application/json";“传输-编码”=标识

Android上的请求看起来如下:

代码语言:javascript
复制
            "{\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" +
            "}";

这是我应该发送的请求:

代码语言:javascript
复制
"{ \"jsonrpc\": \"2.0\", \"id\": 1, \"method\": \"call\", \"params\": [ \"00000000000000000000000000000000\", \"session\", \"login\", { \"username\": \"root\", \"password\": \"admin01\"  } ] }" 

这是我的密码:

代码语言:javascript
复制
   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.
}
EN

回答 1

Stack Overflow用户

发布于 2017-01-10 19:01:31

您的JSON对象似乎无效

有一些非常有用的JSON验证器是免费的。脑海中浮现的是http://jsonlint.com

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

代码语言:javascript
复制
[
    {
    "jsonrpc": 2.0,
    "id": 1,
    "method": "call",
    "params": ["00000000000000", "session", "login"],
    "username": username,
    "password": password
    }
]

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41576229

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档