json - Extracting usable data from an API response in Swift -
i picked project previous dev kind of left mess. we've moved new api better structure , i'm confused how can work.
i've got api i'm trying parse data it's in usable form , i'm new enough use help.
could take @ following code , kind of guide me , explain what's going on here andy maybe how work properly? i've been beating head against wall on 1 few days now.
here's sample of json response 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 should take json response
public class inventory { public var products = [product]() public init(type: product.type, data:[[string:anyobject]]) { 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 price") } } }
you missing code needed load json file , parse it. also, json have posted has illegal characters choke parser. e.g.: { “green shirt": [
first quote curly quote. first, need clean of these up.
then save json along source files in, "data.json". if did that, parse json 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 pull product data parsed json dictionary product objects.
Comments
Post a Comment