Reputation:
I picked up a project from a previous dev that kind of left a mess. We've moved to a new API with a better structure and I'm very confused as to how I can get this to work.
I've got an API that I'm trying to parse the data from so it's in a usable form and I'm new enough that I could use some help.
Could someone take a look at the following code and kind of guide me and explain what's going on here andy maybe how to get it to work properly? I've been beating my head against the wall on this one for a few days now.
Here's a sample of the JSON response that I'm getting:
{
“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 that should take the JSON response
public class Inventory {
public var products = [Product]()
public init(type: Product.Type, data:[[String:AnyObject]]) {
for productData in data {
products.append(type(data: productData))
}
}
}
Product Class
public class Product {
public var data:[String:AnyObject]
required public init(data: [String:AnyObject]) {
self.data = data
}
}
Main Product 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")
}
}
}
Upvotes: 0
Views: 475
Reputation: 776
You are missing the code needed to load the JSON file and parse it. Also, the JSON you have posted has some illegal characters which will choke the parser. e.g.: { “Green Shirt": [
The first quote is a curly quote. First, you need to clean all of these up.
Then you could save the JSON along with your source files in, say "data.json". If you did that, you could parse the JSON like this:
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
Then you could pull the product data from the parsed JSON dictionary into your Product objects.
Upvotes: 1