Prashant Borade
Prashant Borade

Reputation: 3

Parse Using Swifty JSON

My JSON is like:

{
    "status": 1,
    "msg": "Category Product List",
    "product_data": [{
            "product_id": "49",
            "image": "http://192.168.1.78/Linkon/site/pub/static/frontend/Linkon/default/en_US/Magento_Catalog/images/product/placeholder/image.jpg",
            "shopName": "putin",
            "review": "",
            "rating": "2",
            "productName": "ccd",
            "customrFirstName": "devi",
            "customrLastName": "ss",
            "address": "6th Ln, S.T.Colony, Mahalaxminagar, Rajarampuri, Kolhapur, Maharashtra 416008, India",
            "contactNumber": null,
            "description": "<p>ccd</p>"
        },
        {
            "product_id": "50",
            "image": "http://192.168.1.78/Linkon/site/pub/static/frontend/Linkon/default/en_US/Magento_Catalog/images/product/placeholder/image.jpg",
            "shopName": "putin",
            "review": "",
            "rating": "2",
            "productName": "car garage",
            "customrFirstName": "devi",
            "customrLastName": "ss",
            "address": "6th Ln, S.T.Colony, Mahalaxminagar, Rajarampuri, Kolhapur, Maharashtra 416008, India",
            "contactNumber": null,
            "description": "<p>car garage</p>"
        }
    ]
}

So my question is: How to create JSON model class and parse using swifty JSON?

Upvotes: 0

Views: 509

Answers (3)

Khushbu Desai
Khushbu Desai

Reputation: 1023

You can create class using SwiftyJSONAccelerator

enter image description here

Get SwiftyJSONAccelerator from Here: https://github.com/insanoid/SwiftyJSONAccelerator

or you can create it online using https://app.quicktype.io/

enter image description here

Upvotes: 0

Gereon
Gereon

Reputation: 17844

I would recommend ditching SwiftyJSON in favor of the built-in Codable and JSONDecoder support in Swift 4.

For this, you simply define a struct that matches your JSON format, and decode it:

struct Data: Codable {
    let status: Int
    let msg: String
    let products: [Product]

    enum CodingKeys: String, CodingKey {
        case status, msg
        case products = "product_data"
    }
}

struct Product: Codable {
    let product_id, image, shopName, review: String
    let rating, productName, customrFirstName, customrLastName: String
    let address: String
    let contactNumber: String?
    let description: String
}

do {
    let data = try JSONDecoder().decode(Data.self, from: json)
    print("\(data.msg)") // e.g.
} catch {
    print("\(error)")
}

Upvotes: 1

Mridul Gupta
Mridul Gupta

Reputation: 609

You can create your data model class like below:

import UIKit
import SwiftyJSON

class ProductModels: NSObject {
   var productModel:[ProductModel]?
}
public init(json:JSON) {
   self.productModel = json["product_data"].dictionary
}
class ProductModel: NSObject {
var productID:String?
var image:String?
var shopName:String?
var review:String?
var rating:String?
var productName:String?
var customrFirstName:String?
var customrLastName:String?
var address:String?
var contactNumber:String?
var description:String?

public init(json:JSON) {
    self.productID = json["product_id"].string
    self. image = json["image"].string
    self. shopName = json["shopName"].string
    self. review = json["review"].string
    self. rating = json["rating"].string
    self. productName = json["productName"].string
    self. customrFirstName = json["customrFirstName"].string
    self. customrLastName = json["customrLastName"].string
    self. address = json["address"].string
    self. contactNumber = json["contactNumber"].string
    self. description = json["description"].string
    }
}

and can use this class by passing response from model who is calling api and getting this response example below: (the class where you are using this below code, you have to import SwiftyJSON)

Alamofire.request(//(your method or api call).responseJSON(completionHandler: { (response) in
switch response.result {
    case .success(let value):
        let productJson = JSON(value)
        let productsData = ProductModels(json: productJson)
        break;
    }
})

Upvotes: 0

Related Questions