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

Extracting usable data from an API response in Swift

我从以前的开发人员那里接了一个项目,有点乱。我们已经转移到具有更好结构的新 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
        }
    ]
}

Class 应采用 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))
        }
    }
}

产品Class

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

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

主要产品Class

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")
        }
    }
}

您缺少加载 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 字典中的产品数据提取到您的产品对象中。