无法在iOS中使用swift解析json

时间:2015-08-26 06:22:31

标签: ios json swift alamofire

我正试图从json获得价值,但它正在给出这个错误

type of expression is ambiguous without more context

我的ViewController代码是

Alamofire.request(.GET, "http://resturl.com/rest/loginuser",
        parameters: ["password":uPwd,"username": uEmail,], encoding: .JSON).responseJSON() {
            (request,response, data,error) in

            let jsonData = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &error) as? NSDictionary // it is giving error here

            if let parseJson=jsonData{
                var result:String=parseJson[""] as String!;
            }
        }

我搜索过互联网但找不到合适的解决方案。请指导我这个

2 个答案:

答案 0 :(得分:0)

Alamofire.request(.GET, "http://resturl.com/rest/loginuser",
        parameters: ["password":uPwd,"username": uEmail,], encoding: .JSON)
.responseJSON { _, _, JSON, _ in

            if let parseJson=JSON{
              println(parseJson)
            }
        }

答案 1 :(得分:0)

  1. 首先,数据将以json格式返回。所以不需要以json格式转换它。所以请使用我的代码。希望它会对你有所帮助。

  2. 第二件事删除最后一个分号(),这是该行的额外内容。

  3. ["密码":uPwd,"用户名":uEmail,]

    Alamofire.request(.GET, "http://resturl.com/rest/loginuser",
                    parameters: ["password": uPwd,"username": uEmail], encoding: .JSON).responseJSON() {
                        (request,response, data,error) in
    
                        if let jsonData : NSDictionary = data as! [NSDictionary : AnyObject]{
             // do stuff over here
            }
           else{
                  println(data)
                  println(error)
                }
         }