Swift - Create data model from JSON response

2019-03-11 08:40发布

I'm learning Swift lang and one of the things that would be great to hear others input about is "How you handle models from JSON responses"? For example -

I have User.swift model:

class User: NSObject {
  var user_token:String?
  var email:String?
}

and also I would like to use KeyValueObjectMapping as I do in Obj-C projects. Unfortunately this doesn't work here:

let parser = DCKeyValueObjectMapping.mapperForClass(User)
let user = parser.parseDictionary(data.objectForKey("user") as NSDictionary) as User
println(user.user_token) // returns nil

How do you create your models in Swift?

5条回答
冷血范
2楼-- · 2019-03-11 08:55

I recommend using code generation to generate models in Swift based on JSON. To that end, I've created a tool at http://www.guideluxe.com/JsonToSwift to make modeling and parsing JSON as easy as possible.

After you've submited a sample JSON object with a class name to the tool, it will generate a corresponding Swift class, as well as any needed subsidiary Swift classes, to represent the structure implied by the sample JSON. Also included are class methods used to populate Swift objects, including one that utilizes the NSJSONSerialization.JSONObjectWithData method. The necessary mappings from the NSArray and NSDictionary objects are provided.

After copying the generated code into your project as a Swift class(es), you only need to supply an NSData object containing JSON that matches the sample provided to the tool.

Other than Foundation, there are no dependencies.

Here's how to create an NSData object from a JSON file to test with.

let fileUrl: NSURL = NSBundle.mainBundle().URLForResource("JsonFile", withExtension: "json")!
let jsonData: NSData = NSData(contentsOfURL: fileUrl)!
查看更多
可以哭但决不认输i
3楼-- · 2019-03-11 08:59

I would suggest using SwiftyJSONModel there your model would look something like:

import SwiftyJSONModel

class User: NSObject, JSONObjectInitializable {
    enum PropertyKey : String {
        case user_token, email
    }

    var user_token:String?
    var email:String?

    required init(object: JSONObject<PropertyKey>) throws {
        user_token = object.value(for: .user_token)
        email = object.value(for: .email)
    }
}

This library has 3 nice things:

  1. You don't have to explicitly cast to String as library will infer the type
  2. You can have non-optional properties and library will tell you which exact field was wrong
  3. All the keys to the model are incapsulated in enum which gives you auto-complition when you type the keys and guarantees that you cannot access keys, that are not in enum
查看更多
贪生不怕死
4楼-- · 2019-03-11 09:00

Here is some example code for Model Class and parsing JSON response with out any library.

Model Class

class User: NSObject{
     var user_token: String = ""
     var email: String = ""
}

Example code to call web-service api and Parsing.

NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
                var err: NSError
                var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
                //println("Result : \(jsonResult)")
                let model = User()
            model. user_token = jsonResult["user_token"] as NSString
            model. email = jsonResult["email"] as NSString
            })
查看更多
神经病院院长
5楼-- · 2019-03-11 09:03

If you want a more rigorous approach and have access to JSON schemas as meta description for your JSON documents, I wrote a code generator which can handle those (JSON schema, draft 4):

https://github.com/werner77/MappableObjectGenerator

My tools supports basically any programming language, because it is based on code generation templates, but is focussed on ObjectiveC and Swift 4 support right now.

查看更多
可以哭但决不认输i
6楼-- · 2019-03-11 09:15

I'm using jsoncafe easiest and customizable template base model class generator with different framwroks like SwiftyJSON, Codable, Gloss, Simple Swift Class even you can make your own template

jsoncafe.com enter image description here

查看更多
登录 后发表回答