How to map dynamic properties in model class (Swift) - json

I am new to IOS Development, i came across a really interesting situation, i have this json response coming server side
{
"caps": {
"first_key": "34w34",
"first_char": "34w45",
"first_oddo": "34w34"
.... : .....
.... : .....
}
}
My issue this that keys inside "caps" object can be dynamic (like what if one more value is added). I am using ObjectMapper to mapper to map values from response to model class. I have this model class
class User: Mappable {
var first_key: String?
var first_char: String?
var first_oddo: String?
required init?(map: Map) {
}
// Mappable
func mapping(map: Map) {
first_key <- map["first_key"]
first_char <- map["first_char"]
first_oddo <- map["first_oddo"]
}
}
Now as i dont know how to populate my model if values in json response are changed (because that are dynamic). I hope i have explained it well. I think i dont want hard coded values inside model?

This solution uses the Codable protocol introduced with Swift 4.
If the keys of your JSON are dynamic then you need a Dictionary.
JSON
Given this JSON
let data = """
{
"caps": {
"first_key": "34w34",
"first_char": "34w45",
"first_oddo": "34w34"
}
}
""".data(using: .utf8)!
The Response Model
You can define a struct like this
struct Response:Codable {
let caps: [String:String]
}
Decoding 🎉🎉🎉
Now you can decode your JSON
if let response = try? JSONDecoder().decode(Response.self, from: data) {
print(response.caps)
}
Output
["first_key": "34w34", "first_oddo": "34w34", "first_char": "34w45"]

Related

Automatic parsing JSON with Swift 5 and Alomofire

I am calling an API via Alomofire in Swift 5 and get the following Result Set in JSON format :
Here is the call :
AF.request("https://shopping.yahooapis.jp/ShoppingWebService/V1/json/itemSearch", parameters: parameters).responseJSON { response in
print(response.result)
}
I have the following info in the result : Content-Encoding: gzip, the solution may come from here)
Here is the JSON Response body :
{"ResultSet":{"totalResultsAvailable":"3","totalResultsReturned":3,"firstResultPosition":"1","0":{"Result":{"Request":{"Query":""},"Modules":"","0":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_4901330197711f","Condition":"new","Image":{"Id":"ejapan_4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"121"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"121","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"121","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"1","Times":"1","PremiumAmount":"1","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11871","Total":"51092","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"1"}},"1":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711f.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711f","Condition":"new","Image":{"Id":"ejapan_012-4901330197711f","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711f","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711f"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711f"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"1398"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"1398","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"1398","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"13","Times":"1","PremiumAmount":"13","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11875","Total":"51100","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"2"}},"2":{"Name":"\u3010\u30ad\u30e3\u30c3\u30b7\u30e5\u30ec\u30b95\uff05\u9084\u5143\u3011\u3010\u9001\u6599\u7121\u6599\u3011\u2605\u307e\u3068\u3081\u8cb7\u3044\u2605\u3000\u30ab\u30eb\u30d3\u30fc\u3000\u304b\u3063\u3071\u3048\u3073\u305b\u3093\u7d00\u5dde\u306e\u6885\u547370g\u3000\u3000\u00d712\u500b\u3010\u30a4\u30fc\u30b8\u30e3\u30d1\u30f3\u30e2\u30fc\u30eb\u3011","Description":"\u3010\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\uff08\u5546\u54c1\u5185\u5bb9\u3092\u4fdd\u969c\u3059\u308b\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\uff09\u3011\u304a\u83d3\u5b50\u3000\u304a\u3084\u3064\u3000\u30b9\u30ca\u30c3\u30af\u3000\u30b9\u30ca\u30c3\u30af\u83d3\u5b50\u3000\u30ab\u30c3\u30d1\u30a8\u30d3\u30bb\u30f3\u3000\u3046\u3081\u5473\u3000\u30a6\u30e1\u3000\u6d77\u8001\u3000\u30ab\u30eb\u30b7\u30a6\u30e0","Headline":"","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/012-4901330197711fm.html","ReleaseDate":"","Availability":"instock","Code":"ejapan_012-4901330197711fm","Condition":"new","Image":{"Id":"ejapan_012-4901330197711fm","Small":"https:\/\/item-shopping.c.yimg.jp\/i\/c\/ejapan_012-4901330197711fm","Medium":"https:\/\/item-shopping.c.yimg.jp\/i\/g\/ejapan_012-4901330197711fm"},"Review":{"Rate":"0.00","Count":"0","Url":"https:\/\/shopping.yahoo.co.jp\/review\/item\/list?store_id=ejapan&page_key=012-4901330197711fm"},"Affiliate":{"Rate":"1.0"},"Price":{"_attributes":{"currency":"JPY"},"_value":"2278"},"PremiumPrice":"","PriceLabel":{"_attributes":{"taxIncluded":"true"},"FixedPrice":"","DefaultPrice":"2278","SalePrice":"","PremiumPriceStatus":"0","PremiumPrice":"2278","PremiumDiscountType":"","PremiumDiscountRate":"","PeriodStart":"","PeriodEnd":""},"Point":{"Amount":"22","Times":"1","PremiumAmount":"22","PremiumTimes":"1"},"Shipping":{"Code":"1","Name":"\u8a2d\u5b9a\u7121\u3057"},"Category":{"Current":{"Id":"13451","Name":"\u305d\u306e\u4ed6\u30b9\u30ca\u30c3\u30af\u3001\u304a\u83d3\u5b50\u3001\u304a\u3064\u307e\u307f"}},"CategoryIdPath":{"0":{"Id":"1"},"1":{"Id":"2498"},"2":{"Id":"4745"},"3":{"Id":"13451"},"_container":"Category"},"Brands":{"Name":"\u30ab\u30eb\u30d3\u30fc","Path":{"0":{"Id":"1"},"1":{"Id":"16267"},"_container":"Brand"}},"JanCode":"4901330197711","Model":"","IsbnCode":"","Store":{"Id":"ejapan","Name":"e\u30b8\u30e3\u30d1\u30f3","Url":"https:\/\/store.shopping.yahoo.co.jp\/ejapan\/","Payment":{"0":{"Code":"1","Name":"\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"1":{"Code":"16","Name":"Yahoo!\u30a6\u30a9\u30ec\u30c3\u30c8\u306b\u767b\u9332\u3057\u3066\u3044\u308b\u30af\u30ec\u30b8\u30c3\u30c8\u30ab\u30fc\u30c9"},"2":{"Code":"4","Name":"\u5546\u54c1\u4ee3\u5f15"},"3":{"Code":"2","Name":"\u9280\u884c\u632f\u8fbc"},"4":{"Code":"8","Name":"\u90f5\u4fbf\u632f\u66ff"},"5":{"Code":"1024","Name":"\u30bd\u30d5\u30c8\u30d0\u30f3\u30af\u307e\u3068\u3081\u3066\u652f\u6255\u3044"},"6":{"Code":"4096","Name":"PayPay"},"_container":"Method"},"IsBestStore":"false","Ratings":{"Rate":"4.5","Count":"11874","Total":"51099","DetailRate":"4.3"},"Image":{"Id":"ejapan_1","Medium":"https:\/\/item-shopping.c.yimg.jp\/s\/h\/ejapan_1"},"IsPMallStore":"false"},"IsAdult":"0","Deliveryinfo":{"Area":"","Deadline":"","Day":""},"_attributes":{"index":"3"}},"_container":"Hit"}}}}
So I would like to find the way to automatically parse this JSON body in order to exploit the data later,
Does the solution come from the encoding format ?
I have not been successful implementing decompressing using Gzip (link here)
Would it be possible to do it automatically via Decodable or the data is maybe directly accessible in Alomofire ?
Thank you in advance,
Charles
When it comes to decoding and holding data from JSON responses from REST APIs, I have used the following approach:
Include the bellow two PODS
1) Alamofire
2) TRON
Now, you'll have to create a class you'll use as a datasource interface between the API and the actual class model. In the following example, I'm pulling data from a movie database service and I have the bellow class for the data sourcing:
class MovieDataSource: JSONDecodable {
var movies: [Movie] = []
let cellid = "cellid"
required init(json: JSON) throws {
guard let moviesArray = json["results"].array else {
print("Error on the JSON Format")
return
}
self.movies = moviesArray.map({return Movie(json: $0)})
}
}
class JSONError: JSONDecodable {
required init(json: JSON) throws {
print("JSON error\n", json)
}
}
Then I have the actual data holding class/structure:
struct Movie {
let title: String
let movieId: Int
let overview: String
let releaseDate: String
let posterPathURL: String
let genres: [Int]
let vote_average: Float
let popularity: Float
let isForAdults: Bool
init(json: JSON) {
self.title = json["title"].stringValue
self.movieId = json["id"].intValue
self.overview = json["overview"].stringValue
self.releaseDate = json["release_date"].stringValue
self.posterPathURL = json["poster_path"].stringValue
self.genres = json["genre_ids"].arrayObject as! [Int]
self.vote_average = json["vote_average"].floatValue
self.popularity = json["popularity"].floatValue
self.isForAdults = json["adult"].boolValue
}
}
I think the above information should be enough to get you an idea on how to move forward, but If you need further guidance, I found this video series very educative on this subject: https://www.youtube.com/watch?v=tt67VikEovY&list=PL0dzCUj1L5JE1wErjzEyVqlvx92VN3DL5&index=13
Good luck!

how to Converting JSON into Codable in swift 4.2?

I am using Xcode 10.1 and Swift 4.2. When i try to convert JSON response into Codable class it gives an error that Expected to decode Array<Any> but found a string/data instead.
My Actual JSON response is Like this from API .
{
"d": "[{\"Data\":{\"mcustomer\":[{\"slno\":1000000040.0,\"fstname\":null}]},\"Status\":true}]"
}
My Model is Like this
class MainData: Codable{
var d: [SubData]
}
class SubData : Codable {
var Data : Customer
var Status : Bool?
}
class Customer : Codable {
var mcustomer : [Detail]
}
class Detail : Codable {
var slno : Double?
var fstname : String?
}
And I am Decode this Model using JSONDecoder()
let decoder = JSONDecoder()
let deco = try decoder.decode(MainData.self, from: data)
but, I am unable to Decode this Json into My Model.
Your API is wrong. You array in json shouldn't have quotation marks around it. Otherwise you're declaring that value for key "d" is string
"[...]"
[...]
Suggestions:
Variables and constants should start with small capital letter. Otherwise for example your Data property would cause confusion with Data type. For renaming it while decoding you can use CodingKeys
If you don't need to encode your model, you can just implement Decodable protocol
You can use struct instead of class for your model
The top-level JSON object is a dictionary with the key "d" and a string value, representing another JSON object (sometimes called "nested JSON"). If the server API cannot be changed then the decoding must be done in two steps:
Decode the top-level dictionary.
Decode the JSON object from the string obtained in step one.
Together with Robert's advice about naming, CodingKeys and using structs it would look like this:
struct MainData: Codable {
let d: String
}
struct SubData : Codable {
let data : Customer
let status : Bool
enum CodingKeys: String, CodingKey {
case data = "Data"
case status = "Status"
}
}
struct Customer : Codable {
let mcustomer : [Detail]
}
struct Detail : Codable {
let slno : Double
let fstname : String?
}
do {
let mainData = try JSONDecoder().decode(MainData.self, from: data)
let subData = try JSONDecoder().decode([SubData].self, from: Data(mainData.d.utf8))
print(subData)
} catch {
print(error)
}
For your solution to work, the JSON reponse has to be following format
let json = """
{
"d": [
{
"Data": {
"mcustomer": [
{
"slno": 1000000040,
"fstname": null
}
]
},
"Status": true
}
]
}
"""
But, as you can see, the JSON response you are getting is quite different than you are expecting. Either you need to ask to change the response or you need to change your model.

Can I set values for a Swift object properties using mirroring?

Right now I can inspect variables of an object using Mirror type. But can I set values for my variables using mirroring? Or maybe there's another pure-Swift way?
For example, I'd like to create an object (a Swift struct) from JSON. Is it possible without subclassing NSObject and using Objective-C functions for that?
This was the best I can do at the moment. It is still missing converting the mirrorObject back to its generic type. FYI this is using SwiftyJSON
func convertToObject<T>(json: JSON, genericObject: T) -> T {
let mirroredObject = Mirror(reflecting: genericObject)
for (_, var attr) in mirroredObject.children.enumerate() {
if let propertyName = attr.label as String! {
attr.value = json[propertyName]
print(propertyName)
print(attr.value)
}
}
// Figure out how to convert back to object type...
}
This is an old question, but the answer was not working for me.
I had to change my swift object to a NSObject to make things work, and also to have dynamic properties.
In my case I use the pod Marshal to deserialize Data.
class MyClass: NSObject, Unmarshaling
{
// #objc dynamic make property available for NSObject
#objc dynamic var myProperty: String?
required init(object: MarshaledObject) throws {
super.init()
initUsingReflection(object: object)
}
func initUsingReflection(object: MarshaledObject) {
let mirror = Mirror(reflecting: self)
// we go through children
for child in mirror.children {
guard let key = child.label else {
continue
}
// This line is here to get the value from json, in my case I already know the type I needed
let myValue: String = try! object.value(for: key)
// The trick is here, setValue only exist in NSObject and not in swift object.
self.setValue(myValue, forKey: key)
}
}
}

Mapping a JSON object to a Swift class/struct

I need to "replicate" an entiry which is returned from a remote web API service in JSON. It looks like this:
{
"field1": "some_id",
"entity_name" = "Entity1"
"field2": "some name",
"details1": [{
"field1": 11,
"field2": "some value",
"data": {
"key1": "value1",
"key2": "value2",
"key3": "value3",
// any other, unknown at compile time keys
}
}],
"details2": {
"field1": 13,
"field2": "some value2"
}
}
Here's my attempt:
struct Entity1 {
struct Details1 {
let field1: UInt32
let field2: String
let data: [String: String]
}
struct Details2 {
let field1: UInt32
let field2: String
}
let field1: String
static let entityName = "Entity1"
let field2: String
let details1: [Details1]
let details2: Details2
}
Is it a good idea to use structs instead of classes for such a goal
as mine?
Can I anyhow define a nested struct or a class, say
Details1 and create a variable of it at the same time?
Like this:
//doesn't compile
struct Entity1 {
let details1: [Details1 {
let field1: UInt32
let field2: String
let data: [String: String]
}]
You can use any if the following good open-source libraries available to handle the mapping of JSON to Object in Swift, take a look :
Mapper
ObjectMapper
JSONHelper
Argo
Unbox
Each one have nice a good tutorial for beginners.
Regarding the theme of struct or class, you can consider the following text from The Swift Programming Language documentation:
Structure instances are always passed by value, and class
instances are always passed by reference. This means that they are
suited to different kinds of tasks. As you consider the data
constructs and functionality that you need for a project, decide
whether each data construct should be defined as a class or as a
structure.
As a general guideline, consider creating a structure when one or more
of these conditions apply:
The structure’s primary purpose is to encapsulate a few relatively simple data values.
It is reasonable to expect that the encapsulated values will be copied rather than referenced when you assign or pass around an
instance of that structure.
Any properties stored by the structure are themselves value types, which would also be expected to be copied rather than referenced.
The structure does not need to inherit properties or behavior from another existing type.
Examples of good candidates for structures include:
The size of a geometric shape, perhaps encapsulating a width property and a height property, both of type Double.
A way to refer to ranges within a series, perhaps encapsulating a start property and a length property, both of type Int.
A point in a 3D coordinate system, perhaps encapsulating x, y and z properties, each of type Double.
In all other cases, define a class, and create instances of that class
to be managed and passed by reference. In practice, this means that
most custom data constructs should be classes, not structures.
I hope this help you.
HandyJSON is exactly what you need. See code example:
struct Animal: HandyJSON {
var name: String?
var id: String?
var num: Int?
}
let jsonString = "{\"name\":\"cat\",\"id\":\"12345\",\"num\":180}"
if let animal = JSONDeserializer.deserializeFrom(json: jsonString) {
print(animal)
}
https://github.com/alibaba/handyjson
Details
Xcode 10.2.1 (10E1001), Swift 5
Links
Pods:
Alamofire - loading data
More info:
Codable
More samples of usage Codable and ObjectMapper in Swift 5
Task
Get itunes search results using iTunes Search API with simple request https://itunes.apple.com/search?term=jack+johnson
Full sample
import UIKit
import Alamofire
// Itunce api doc: https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/#searching
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
loadData()
}
private func loadData() {
let urlString = "https://itunes.apple.com/search?term=jack+johnson"
Alamofire.request(urlString).response { response in
guard let data = response.data else { return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let result = try decoder.decode(ItunceItems.self, from: data)
print(result)
} catch let error {
print("\(error.localizedDescription)")
}
}
}
}
struct ItunceItems: Codable {
let resultCount: Int
let results: [ItunceItem]
}
struct ItunceItem: Codable {
var wrapperType: String?
var artistId: Int?
var trackName: String?
var trackPrice: Double?
var currency: String?
}
you could use SwiftyJson and let json = JSONValue(dataFromNetworking)
if let userName = json[0]["user"]["name"].string{
//Now you got your value
}
Take a look at this awesome library that perfectly fits your need, Argo on GitHub.
In your case, a struct is ok. You can read more on how to choose between a struct and a class here.
You can go with this extension for Alamofire https://github.com/sua8051/AlamofireMapper
Declare a class or struct:
class UserResponse: Decodable {
var page: Int!
var per_page: Int!
var total: Int!
var total_pages: Int!
var data: [User]?
}
class User: Decodable {
var id: Double!
var first_name: String!
var last_name: String!
var avatar: String!
}
Use:
import Alamofire
import AlamofireMapper
let url1 = "https://raw.githubusercontent.com/sua8051/AlamofireMapper/master/user1.json"
Alamofire.request(url1, method: .get
, parameters: nil, encoding: URLEncoding.default, headers: nil).responseObject { (response: DataResponse<UserResponse>) in
switch response.result {
case let .success(data):
dump(data)
case let .failure(error):
dump(error)
}
}

Automatic JSON serialization and deserialization of objects in Swift

I'm looking for a way to automatically serialize and deserialize class instances in Swift. Let's assume we have defined the following class …
class Person {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
… and Person instance:
let person = Person(firstName: "John", lastName: "Doe")
The JSON representation of person would be the following:
{
"firstName": "John",
"lastName": "Doe"
}
Now, here are my questions:
How can I serialize the person instance and get the above JSON without having to manually add all properties of the class to a dictionary which gets turned into JSON?
How can I deserialize the above JSON and get back an instantiated object that is statically typed to be of type Person? Again, I don't want to map the properties manually.
Here's how you'd do that in C# using Json.NET:
var person = new Person("John", "Doe");
string json = JsonConvert.SerializeObject(person);
// {"firstName":"John","lastName":"Doe"}
Person deserializedPerson = JsonConvert.DeserializeObject<Person>(json);
As shown in WWDC2017 # 24:48 (Swift 4), we will be able to use the Codable protocol. Example
public struct Person : Codable {
public let firstName:String
public let lastName:String
public let location:Location
}
To serialize
let payload: Data = try JSONEncoder().encode(person)
To deserialize
let anotherPerson = try JSONDecoder().decode(Person.self, from: payload)
Note that all properties must conform to the Codable protocol.
An alternative can be JSONCodable which is used by Swagger's code generator.
You could use EVReflection for that. You can use code like:
var person:Person = Person(json:jsonString)
or
var jsonString:String = person.toJsonString()
See the GitHub page for more detailed sample code. You only have to make EVObject the base class of your data objects. No mapping is needed (as long as the json keys are the same as the property names)
Update: Swift 4 has support for Codable which makes it almost as easy as EVReflection but with better performance. If you do want to use an easy contractor like above, then you could use this extension: Stuff/Codable
With Swift 4, you simply have to make your class conform to Codable (Encodable and Decodable protocols) in order to be able to perform JSON serialization and deserialization.
import Foundation
class Person: Codable {
let firstName: String
let lastName: String
init(firstName: String, lastName: String) {
self.firstName = firstName
self.lastName = lastName
}
}
Usage #1 (encode a Person instance into a JSON string):
let person = Person(firstName: "John", lastName: "Doe")
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted // if necessary
let data = try! encoder.encode(person)
let jsonString = String(data: data, encoding: .utf8)!
print(jsonString)
/*
prints:
{
"firstName" : "John",
"lastName" : "Doe"
}
*/
Usage #2 (decode a JSON string into a Person instance):
let jsonString = """
{
"firstName" : "John",
"lastName" : "Doe"
}
"""
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let person = try! decoder.decode(Person.self, from: jsonData)
dump(person)
/*
prints:
â–¿ __lldb_expr_609.Person #0
- firstName: "John"
- lastName: "Doe"
*/
There is a Foundation class called NSJSONSerialization which can do conversion to and from JSON.
The method for converting from JSON to an object looks like this:
let jsonObject = NSJSONSerialization.JSONObjectWithData(data,
options: NSJSONReadingOptions.MutableContainers,
error: &error) as NSDictionary
Note that the first argument to this method is the JSON data, but not as a string object, instead as a NSData object (which is how you'll often times get JSON data anyway).
You most likely will want a factory method for your class that takes JSON data as an argument, makes use of this method and returns an initialize object of your class.
To inverse this process and create JSON data out of an object, you'll want to make use of dataWithJSONObject, in which you'll pass an object that can be converted into JSON and have an NSData? returned. Again, you'll probably want to create a helper method that requires no arguments as an instance method of your class.
As far as I know, the easiest way to handle this is to create a way to map your objects properties into a dictionary and pass that dictionary for turning your object into JSON data. Then when turning your JSON data into the object, expect a dictionary to be returned and reverse the mapping process. There may be an easier way though.
You can achieve this by using ObjectMapper library. It'll give you more control on variable names and the values and the prepared JSON. After adding this library extend the Mappable class and define mapping(map: Map) function.
For example
class User: Mappable {
var id: Int?
var name: String?
required init?(_ map: Map) {
}
// Mapping code
func mapping(map: Map) {
name <- map["name"]
id <- map["id"]
}
}
Use it like below
let user = Mapper<User>().map(JSONString)
First, create a Swift object like this
struct Person {
var firstName: String?;
var lastName: String?;
init() {
}
}
After that, serialize your JSON data you retrieved, using the built-in NSJSONSerialization and parse the values into the Person object.
var person = Person();
var error: NSError?;
var response: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: &error);
if let personDictionary = response as? NSDictionary {
person.firstName = personDictionary["firstName"] as? String;
person.lastName = personDictionary["lastName"] as? String;
}
UPDATE:
Also please take a look at those libraries
Swift-JsonSerialiser
ROJSONParser
Take a look at NSKeyValueCoding.h, specifically setValuesForKeysWithDictionary. Once you deserialize the json into a NSDictionary, you can then create and initialize your object with that dictionary instance, no need to manually set values on the object. This will give you an idea of how the deserialization could work with json, but you will soon find out you need more control over deserialization process. This is why I implement a category on NSObject which allows fully controlled NSObject initialization with a dictionary during json deserialization, it basically enriches the object even further than setValuesForKeysWithDictionary can do. I also have a protocol used by the json deserializer, which allows the object being deserialized to control certain aspects, for example, if deserializing an NSArray of objects, it will ask that object what is the type name of the objects stored in the array.