从Swift中的API响应中提取可用数据

时间:2015-06-04 17:02:36

标签: json swift nsdictionary

我从之前的开发中选择了一个项目,这让我感到很乱。我们已经转移到具有更好结构的新API,我对如何使其工作非常困惑。

我有一个API,我试图解析数据,因此它是一个可用的形式,而且我足够新,以至于我可以使用一些帮助。

有人可以看一下下面的代码,并指导我并解释一下这里发生了什么,以及如何让它正常工作?现在我已经在这个墙上撞了我的头几天了。

以下是我获得的JSON响应示例:

{
    “Green Shirt": [
        {
            "id": "740",
            "name": “Nice Green Shirt",
            "quantity": "0",
            "make": "",
            "model": "",
            "price": “15.00",
            "size": "XXS",
            "sku": null,
            "image": "https:\/\/google.com\/green_shirt.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
        },
        {
            "id": "743",
            "name": "Green Shirt",
            "quantity": “68",
            "make": "",
            "model": "",
            "price": “20.00",
            "size": "XS",
            "sku": null,
            "image": "https:\/\/google.com\/green_shirt.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
        }
    ],
    “Dark Blue Jeans": [
        {
            "id": "1588",
            "name": "Dark Blue Jeans",
            "quantity": "0",
            "make": "",
            "model": "",
            "price": "0.00",
            "size": “S",
            "sku": null,
            "image": "https:\/\/google.com\/dark_blue_jeans.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
            "category": null
        },
        {
            "id": "1559",
            "name": "Dark Blue Jeans",
            "quantity": "4",
            "make": "",
            "model": "",
            "price": "0.00",
            "size": “XL",
            "sku": null,
            "image": "https:\/\/google.com\/dark_blue_jeans.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
            "category": null
        }
    ],
    “White Belt": [
        {
            "id": "1536",
            "name": "White Belt",
            "quantity": "37",
            "make": "",
            "model": "",
            "price": "0.00",
            "size": "One Size",
            "sku": null,
            "image": "https:\/\/google.com\/white_belt.jpg",
            "new_record": false,
            "category_name": "",
            "bar_code": "",
            "category": null
        }
    ]
}

应该采取JSON响应的类

public class Inventory {
    public var products = [Product]()

    public init(type: Product.Type, data:[[String:AnyObject]]) {
        for productData in data {
            products.append(type(data: productData))
        }
    }
}

产品类

public class Product {
    public var data:[String:AnyObject]

    required public init(data: [String:AnyObject]) {
        self.data = data
    }
}

主要产品类别

class MainProduct : Product {
    var id:String? {
        return data["id"] as? String
    }

    var name:String {
        return data["model"] as! String
    }

    var sizes:[String:Int] {
        if let items = data["quantities"] as? [String:Int] {
            return items
        } else {
            return [String:Int]()
        }
    }

    var upc :String? {
        return data["UPC"] as? String
    }

    var price:Double {
        if let price = data["price"] as? Double {
            return price
        } else if let price = data["price"] as? NSString {
            return price.doubleValue
        } else {
            fatalError("Did not get a price")
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您缺少加载JSON文件并解析它所需的代码。此外,您发布的JSON有一些非法字符会阻塞解析器。例如:{ “Green Shirt": [第一个引用是卷曲的引用。首先,你需要清理所有这些。

然后你可以将JSON和你的源文件一起保存在" data.json"中。如果你这样做,你可以像这样解析JSON:

var error: NSError? = nil let path = NSBundle.mainBundle().pathForResource("data", ofType: "json") let jsonData = NSData(contentsOfFile: path!, options: .DataReadingMappedIfSafe, error: &error) let jsonResult = NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.AllowFragments, error: &error) as! NSDictionary

然后,您可以将解析后的JSON字典中的产品数据提取到Product对象中。