How generate model class by json(Dictionary) or add properties to existing model class with no property

1.3k Views Asked by At

I spent so much time in writing meaningless code to generate model classes. For example: here is json data

    {
  "id": 1,
  "productcode": "5012014032",
  "productname": "通用点数",
  "categoryid": null,
  "supplierid": null,
  "inprice": null,
  "unit": "¥",
  "barcode": "2546138769542",
  "outprice": 1500,
  "safestock": null,
  "bdate": 1483200000000,
  "type": null,
  "detail": "这是通用点数",
  "ext": null,
  "ext2": null,
  "points": "15",
  "validmonth": 3,
  "photoid": 2,
  "path": "/upload/26-9449705-1477983341-.jpg",
  "productid": 1
}

Then I create class Product:

class Product: NSObject {
let id: String
let productcode: String
let productname: String
let categoryid: String
let supplierid: String
let inprice: String
let unit: String
let barcode: String
let outprice: String
let safestock: String
let bdate: String
let type: String
let detail: String
let ext: String
let ext2: String
let points: String
let validmonth: String
let photoid: String
let path: String
let productid: String

init(id: String, productcode: String, productname: String, categoryid: String, supplierid: String, inprice: String, unit: String, barcode: String, outprice: String, safestock: String, bdate: String, type: String, detail: String, ext: String, ext2: String, points: String, validmonth: String, photoid: String, path: String, productid: String) {
    self.id = id
    self.productcode = productcode
    self.productname = productname
    self.categoryid = categoryid
    self.supplierid = supplierid
    self.inprice = inprice
    self.unit = unit
    self.barcode = barcode
    self.outprice = outprice
    self.safestock = safestock
    self.bdate = bdate
    self.type = type
    self.detail = detail
    self.ext = ext
    self.ext2 = ext2
    self.points = points
    self.validmonth = validmonth
    self.photoid = photoid
    self.path = path
    self.productid = productid
}}

It's too boring.Now I want to know if it possible to generate corresponding class by json programmatically or add property to class with no property.

Hope you know what i said.Swift and objective-C are all acceptable.

Thanks for any advice.

2

There are 2 best solutions below

1
On

The best way is using Swifty JSON Accelerator

Just pass your valid JSON Dictionary it will return model file.

Sample availbale on Github:

https://github.com/insanoid/SwiftyJSONAccelerator

The tool can be downloaded from :

https://github.com/insanoid/SwiftyJSONAccelerator/releases/download/v1.4.0/SwiftyJSONAccelerator.zip

0
On

Please refer to the below link and just pass your JSON data in this app.

https://github.com/Ahmed-Ali/JSONExport

1) Clone the Project.

2) Run the project in Xcode.

3) Paste your JSON response in the application which you have run.

4) Now press save button.

5) You got the model swift file.

We can also use a website to generate a model.

http://www.jsoncafe.com