0

Here i had written model class and all but after if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject] { after this line it was exiting from if loop and not returning any data can anyone help me how to resolve this ?

 var relatedProductsModel : RelatedProductsViewed ?
 var relatedProductsApi = "http://www.json-generator.com/api/json/get/ckagXVRLvS?indent=2"

here is my view did load code

 self.relatedProductsDownloadJsonWithUrl(relatedApi: relatedProductsApi)

here is my json function for getting data

func relatedProductsDownloadJsonWithUrl(relatedApi: String){
        print(relatedApi)
        let url = URL(string: relatedApi)!
        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            if error != nil { print(error!); return }
            do {
                if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? [String:AnyObject] {
                    self.relatedProductsModel = RelatedProductsViewed(dict: jsonObj as [String : AnyObject])
                    DispatchQueue.main.async {
                         print(self.relatedProductsModel)
                    }
                }
            } catch {
                print(error)
            }
        }
        task.resume()
    }
struct RelatedProductsViewed {

    let productId : Int
    let productName : String
    let productSku : String
    let productPrice : Int
    let productsCount : Int
    var relatedProducts = [RelatedProducts]()

    init(dict : [String:Any]) {
        if let arr = dict["related_products"] as? [[String: AnyObject]]{
            var filterArr = [RelatedProducts]()
            for obj in arr {
                filterArr.append(RelatedProducts(dict: obj)!)
            }
            self.relatedProducts = filterArr
        } else {
            self.relatedProducts = [RelatedProducts]()
        }
        self.productId = (dict["product_id"] as? Int)!
        self.productName = (dict["product_name"] as? String)!
        self.productSku = (dict["product_sku"] as? String)!
        self.productPrice = (dict["product_price"] as? Int)!
        self.productsCount = (dict["related_products_count"] as? Int)!
    }
}
  • 2
    In **your** linked question there is the answer. Please learn to read JSON. `{}` is dictionary, `[]` is array. It's very easy. Did you ever read the suggested [Correctly parse JSON in Swift 3](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3) – vadian Oct 12 '17 at 04:58
  • no i am reading now @vadian –  Oct 12 '17 at 05:14
  • thanks working @vadian –  Oct 12 '17 at 06:44
  • @vadian: Could you please fix the dupe target to point directly to [here](https://stackoverflow.com/questions/39423367/correctly-parsing-json-in-swift-3), and not to the deleted question it currently points to? – Ilmari Karonen Nov 09 '17 at 02:11

1 Answers1

-1

Your API returns JSON object with array as root. Casting it to Array of Dictionary should work:

if let jsonObj = try JSONSerialization.jsonObject(with: data!) as? Array<[String:Any]>, let jsonDict = jsonObj.first {
 self.relatedProductsModel = RelatedProductsViewed(dict: jsonDict)
 .....
}

I know the requirement is for Swift 3 but in Swift 4 Swift team has introduced Decodable protocol to decode json objects to structs/classes. I used it to create an array of your struct object.

struct RelatedProductsViewed {
    let productId : Int
    let productName : String
    let productSku : String
    let productPrice : Int
    let productsCount : Int
    let relatedProducts:[RelatedProducts]
}

extension RelatedProductsViewed:Decodable {
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: RelatedProductsViewedKeys.self)
        let productId = try container.decode(Int.self, forKey: .productId)
        let productName = try container.decode(String.self, forKey: .productName)
        let productSku = try container.decode(String.self, forKey: .productSku)
        let productPrice = try container.decode(Int.self, forKey: .productPrice)
        let productsCount = try container.decode(Int.self, forKey: .productsCount)
        let relatedProducts = try container.decode([RelatedProducts].self, forKey: .relatedProducts)

        self.init(productId: productId, productName: productName, productSku: productSku, productPrice: productPrice, productsCount: productsCount, relatedProducts: relatedProducts)
    }

    enum RelatedProductsViewedKeys: String, CodingKey {
        case productId = "product_id"
        case productName = "product_name"
        case productSku = "product_sku"
        case productPrice = "product_price"
        case productsCount = "related_produts_count"
        case relatedProducts = "related_produts"
    }
}

struct RelatedProducts {
    let relatedProductId:Int
    let relatedProductPosition:Int
    let relatedProductStatus:Int
    let relatedProductUrl:URL
    let relatedProductName:String
    let imageUrl:URL
    let relatedProductSku:String
    let relatedProductPrice:Int
}

extension RelatedProducts:Decodable {

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: RelatedProductsKeys.self)
        let relatedProductId = try container.decode(Int.self, forKey: .relatedProductId)
        let relatedProductPosition = try container.decode(Int.self, forKey: .relatedProductPosition)
        let relatedProductStatus = try container.decode(Int.self, forKey: .relatedProductStatus)
        let relatedProductUrl = try container.decode(URL.self, forKey: .relatedProductUrl)
        let relatedProductName = try container.decode(String.self, forKey: .relatedProductName)
        let imageUrl = try container.decode(URL.self, forKey: .imageUrl)
        let relatedProductSku = try container.decode(String.self, forKey: .relatedProductSku)
        let relatedProductPrice = try container.decode(Int.self, forKey: .relatedProductPrice)
        self.init(relatedProductId: relatedProductId, relatedProductPosition: relatedProductPosition, relatedProductStatus: relatedProductStatus, relatedProductUrl: relatedProductUrl, relatedProductName: relatedProductName, imageUrl: imageUrl, relatedProductSku: relatedProductSku, relatedProductPrice: relatedProductPrice)
    }

    enum RelatedProductsKeys: String, CodingKey {
        case relatedProductId = "related_product_id"
        case relatedProductPosition = "related_product_position"
        case relatedProductStatus = "related_product_status"
        case relatedProductUrl = "related_product_url"
        case relatedProductName = "related_product_name"
        case imageUrl = "image_url"
        case relatedProductSku = "related_product_sku"
        case relatedProductPrice = "related_product_price"
    }
}

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        RelatedProductsVM().downloadJsonWithUrl("http://www.json-generator.com/api/json/get/ckagXVRLvS?indent=2")
    }


}

class RelatedProductsVM {
    var relatedProductsViewed:[RelatedProductsViewed]?

    func downloadJsonWithUrl(_ relatedApi: String) {
        print(relatedApi)
        guard let url = URL(string: relatedApi) else {
            return
        }

        let task = URLSession.shared.dataTask(with: url) {[weak self] (data, response, error) in
            guard let strongSelf = self else {
                return
            }
            guard error == nil, let data = data else {
                print(error!)
                return
            }
            strongSelf.relatedProductsViewed = strongSelf.productsViewedFrom(data)
        }
        task.resume()
    }

    func productsViewedFrom(_ data:Data) -> [RelatedProductsViewed]? {
        return try? JSONDecoder().decode(Array<RelatedProductsViewed>.self, from: data)
    }
}
Puneet Sharma
  • 9,369
  • 1
  • 27
  • 33