快速解析JSON数据

时间:2015-09-09 02:12:54

标签: json swift parsing

所以我试图了解JSON解析,我想从这些字段中提取一些信息..

        index = 90;
        property1 =             {
            href = "http://www.bodybuilding.com/exercises/detail/view/name/supine-one-arm-overhead-throw";
            text = "Supine One-Arm Overhead Throw";
        };
        property2 =             {
            href = "http://www.bodybuilding.com/exercises/finder/lookup/filter/muscle/id/13/muscle/abdominals";
            text = Abdominals;
        };
        property3 =             (
                            {
                href = "http://www.bodybuilding.com/exercises/detail/view/name/supine-one-arm-overhead-throw";
                src = "http://www.bodybuilding.com/exercises/exerciseImages/sequences/839/Male/m/839_1.jpg";
                text = "";
            },

我可以获得一大块数据,问题是当我尝试对这些信息进行排序时......这是我的代码

func parseDictionary(dictionary: [String: AnyObject]) {
    if let array: AnyObject = dictionary["results"] {
        for resultDict in array as![AnyObject] {
            if let resultDict = resultDict as? [String:AnyObject] {
                if let wrapperType = resultDict["wrapperType"] as? String {
                    if let kind = resultDict["kind"] as? String {
                        print("wrapperType: \(wrapperType), kind: \(kind)")
                    }
                }

            } else {
                print("expected a dictionary")
            }
        }
    } else {
        print("expected results array")

    }
}

我得到的错误是..

//无法将'__NSCFDictionary'类型的值(0x1014c8a60)强制转换为//'NSArray'(0x1014c8470)。

1 个答案:

答案 0 :(得分:-1)

你的专栏:

for resultDict in array as![AnyObject] {

需要改为

for resultDict in array as![String: AnyObject] {

[AnyObject]Array<AnyObject>的简写,而[String: AnyObject]Dictionary<String, AnyObject>的简写,这解释了您的错误。