序列化NSDictionary时出错

时间:2016-11-13 21:30:56

标签: ios json swift

我试图序列化NSDictionary并收到以下错误:无法读取数据,因为格式不正确。

这是我尝试序列化的一小部分对象。它的格式相同,我刚刚删除了大部分项目。

{
    action = Websites;
    method = "school_webpage";
    result =     {
        CurrentStatus = 1;
        Description = OK;
        ReturnValue =         {
            bgcolor = "#FFFFFF";
            splash = "";
            tree =             (
                                {
                    children =                     (
                                                {
                            iconCls = "icn-add";
                            id = 462793;
                            leaf = 1;
                            showAdd = 0;
                            text = "Ahrens, Linda";
                            type = website;
                        },
                                                {
                            iconCls = "icn-add";
                            id = 498858;
                            leaf = 1;
                            showAdd = 0;
                            text = "Altemose, David";
                            type = website;
                        },
                                                {
                            iconCls = "icn-add";
                            id = 11159347;
                            leaf = 1;
                            showAdd = 0;
                            text = "Alvin, Patrick";
                            type = website;
                        }
}

我的代码如下。此NSDictionary作为AnyObject传递

func parseJson(_ JsonDict: AnyObject)
{
    print(JsonDict)
    var data = [newsarticle]()
    do{
        let jsonData = NSKeyedArchiver.archivedData(withRootObject: JsonDict)
            let jsonObject = try JSONSerialization.jsonObject(with: jsonData,       options: JSONSerialization.ReadingOptions.mutableContainers )

            if let dict = jsonObject as? [String: AnyObject]
            {
                if let dict2 = (dict as? [String : AnyObject])?["result"]?["ReturnValue"]{

                    if let dict3 = (dict2 as! [String: AnyObject])["tree"]  as? [AnyObject] {

                        for dict4  in dict3 {
                            if let dict5 = dict4["children"]  as? [AnyObject]{
                                for dict6 in dict5{
                                    data.append(newsarticle(name: ((dict6 as? [String : AnyObject])?["text"])! as! String,desc: "https://www.oncoursesystems.com/school/webpage/\((dict6 as? [String : AnyObject])?["id"])/689493"))
                                }

                            }
                        }
                        addData(data)
                    }
                }


            }


        }

    catch let error as NSError {
        print("Failed to load: \(error.localizedDescription)")

    }
}

1 个答案:

答案 0 :(得分:0)

这很难理解。我建议您花些时间清理代码,例如:

if let dict = jsonObject as? [String: AnyObject] {
    if let dict2 = (dict as? [String : AnyObject])?["result"]?["ReturnValue"] {

    }
}

可以改写为:

if let dict = jsonObject as? [String: AnyObject] {
    if let returnValue = dict["result"]?["ReturnValue"] {

    }
}

因为您已在第一行中将dict解包为[String: AnyObject]。并使用更多描述性变量名称,因为dict5无法帮助您了解您在尝试解析事物时所处的位置。

所以看一下这一行,try语句就是造成你错误的原因。

let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers )

它基本上是在说,无论你传递的是什么jsonData都不是真正的json。我建议您通过jsonlint.com这样的网站运行jsonData内容,然后修改格式直到它有效。