Swift 4+SwiftyJson convert/generate objects from JSON automatically
Asked Answered
C

3

5

I'm using SwiftyJSON to get a JSON from my web service. What's the easiest way to convert this into a swift object represenation?

I mean maybe a website like http://www.jsonschema2pojo.org/ which does the same for Java.

I so far found generators which work from String or Dictionary but not SwiftyJSON?

Thanks

Consultation answered 5/6, 2018 at 1:29 Comment(2)
The best option is to get rid of SwiftyJSON and use the built in JSONDecoder and Decodable.Stepdame
I think you might be rightConsultation
K
6

You can use json4swift

enter image description here

Online JSON to Swift Models Generator

Our motive is simple, in this age of technology; convenience and automation, working with JSON for Swift shouldn't be complicated anymore. Without dependency on other tools and framework, this free online utility maps your web service responses to appropriate models. It makes working with JSON easier and manageable.

Free Utility

This online free utility generates a Swift 2.0 and Swift 4.0 compatible models which can be simply dragged & used in your project.

Map Response

Simply map your web service response with your models with a single line.

Dictionary Representation

The objects can be referred to as a dictionary anytime should you need them with the current state and same key-value pairs.

Krieg answered 5/6, 2018 at 9:0 Comment(0)
R
11

You can see JSON Master, Here you can generate you code directly from JSON for SwiftyJSON, Codable framework or classical Dictionary. It can also generate struct or class.

Also it has support for Java, Kotlin and C#

Roehm answered 17/10, 2018 at 5:34 Comment(1)
Way better than the other options found in a quick google search. None of the options actually provided full encode() and decode() support except for this, and I tried at least 5.Tremayne
K
6

You can use json4swift

enter image description here

Online JSON to Swift Models Generator

Our motive is simple, in this age of technology; convenience and automation, working with JSON for Swift shouldn't be complicated anymore. Without dependency on other tools and framework, this free online utility maps your web service responses to appropriate models. It makes working with JSON easier and manageable.

Free Utility

This online free utility generates a Swift 2.0 and Swift 4.0 compatible models which can be simply dragged & used in your project.

Map Response

Simply map your web service response with your models with a single line.

Dictionary Representation

The objects can be referred to as a dictionary anytime should you need them with the current state and same key-value pairs.

Krieg answered 5/6, 2018 at 9:0 Comment(0)
T
0

Let's say for example you have a Student Model and you want to map the JSON Object to Student Object. You can try like this:

struct Student {

  let id: Int!
  let name: String!

  init(param: JSON) {
    id = param["id"].intValue
    name = param["name"].stringValue
  }

  /* JSON Response
  {
    "id": 168,
    "name": "KoingDev"
  }
  */

}

Hope it is useful! :)

Topgallant answered 5/6, 2018 at 4:22 Comment(1)
Well yeah, its good if you have simple classes with few parameters. I have 50 with dozens of parameters each. hence generally I'm using generative tools to do stuff like this.Consultation

© 2022 - 2024 — McMap. All rights reserved.