【问题标题】:Swifty JSON Not Working After Updating to Xcode 8更新到 Xcode 8 后 Swifty JSON 不工作
【发布时间】:2016-09-21 06:18:14
【问题描述】:

我的JSON回复是

{
    code = 1;
    document =     (
                {
            Address1 = "<null>";
            Address2 = "<null>";
            City = "<null>";
            CompanyID = 1;
            CompanyName = Innohabit;
            ContactNumber = "<null>";
            Designation = "<null>";
            DesignationID = 36;
            DesignationName = dev;
            Email = "uday.mishra@Gmail.com";
            FCMRegistrationToken = "";
            GCMRegistrationToken = "";
            Gender = Male;
            ID = 9;
            IsActive = 1;
            IsFirstLogin = 1;
            LastLogin = "2016-09-21 05:45:33";
            Name = "Uday Mishra";
            Password = "";
            ProfilePic = "";
            RegisterationDate = "2016-03-04 07:42:42";
            UserTwitter = "<null>";
            Username = "uday.mishra@Gmail.com";
        }
    );
    message = "Record found";
    status = success;
}

并尝试使用Swifty Json 转换响应像这样

            case .success:
            print(response)

            let jsonResponse = JSON(response.data)
            print("JSON")
            let jsonData = jsonResponse["document"]
            print(jsonResponse)
            print(jsonData[0])

但我没有得到想要的结果,输出是

{
    code = 1;
    document =     (
                {
            Address1 = "<null>";
            Address2 = "<null>";
            City = "<null>";
            CompanyID = 1;
            CompanyName = Innohabit;
            ContactNumber = "<null>";
            Designation = "<null>";
            DesignationID = 36;
            DesignationName = dev;
            Email = "uday.mishra@Gmail.com";
            FCMRegistrationToken = "";
            GCMRegistrationToken = "";
            Gender = Male;
            ID = 9;
            IsActive = 1;
            IsFirstLogin = 1;
            LastLogin = "2016-09-21 05:45:33";
            Name = "Uday Mishra";
            Password = "";
            ProfilePic = "";
            RegisterationDate = "2016-03-04 07:42:42";
            UserTwitter = "<null>";
            Username = "uday.mishra@Gmail.com";
        }
    );
    message = "Record found";
    status = success;
}
JSON
unknown
null

您可以看到响应正在打印,但是 print(jsonResponse)print(jsonData[0]) 正在打印 Unknownnull 谁能帮助我如何将数据从DataRespose 正确序列化为JSON 格式

请提出任何替代方法,或者如果我做错了,请帮助我。

【问题讨论】:

  • pod 'SwiftyJSON', :git => 'github.com/appsailor/SwiftyJSON.git', :branch => 'swift3'
  • 我在我的 pod 文件中使用相同的东西
  • 仍然没有得到请帮助。
  • 有没有其他选择。
  • 你试过 SwiftJSON 吗?

标签: ios json xcode swifty-json


【解决方案1】:

尝试使用下面的代码:

if let jsonResponse: AnyObject = JSON(response.data){   
    let jsonData = jsonResponse["document"]
    print(jsonResponse)
    print(jsonData[0])
}

编辑:

我在this阅读了以下声明

目标 Pod 的平台 (iOS 7.1) 与 SwiftyJSON (2.1.3) 不兼容,SwiftyJSON (2.1.3) 的最低要求为 OS X 10.9 - iOS 8.0。 编辑: 我将 SwiftyJson.swift 替换为 Xcode 6.3 分支中的那个,现在它运行良好,没有错误。 :-)

所以试着按照那个步骤来。

【讨论】:

  • 我收到错误:- 无法从“JSON”向下转换为更可选的类型“[String : AnyObject]!”
【解决方案2】:

我试过了,对我来说效果很好。

Alamofire.request(url, method: .post,parameters: parameters).validate().responseJSON { response in
                switch response.result {
                case .success(let data):
                    //print(response)

                    let jsonResponse = JSON(data)
                    print("JSON")
                    let jsonData = jsonResponse["document"][0]
                    print("Email")
                    print(jsonData["Email"])



                case .failure(let error):
                    print(error)
                }
            }

【讨论】:

  • 我的朋友“Alamofire”已经开始工作了。您需要 SwiftyJson 中的解决方案。
猜你喜欢
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2017-02-20
  • 2014-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
相关资源
最近更新 更多