I am experiencing an error while parsing a json
"hoursOfOperation" : {
"sat" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1020,
"open" : 600
}
}
]
},
"fri" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"sun" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1020,
"open" : 600
}
}
]
},
"mon" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 960
}
}
]
},
"tue" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"wed" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"thu" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
}
}
This is JSON shared. I'm using JSONSerialization for parsing the same. Code is as below:
struct HoursOfOperation{
var sun : hoursOfOperationData?
var mon : hoursOfOperationData?
var tue : hoursOfOperationData?
var wed : hoursOfOperationData?
var thu : hoursOfOperationData?
var fri : hoursOfOperationData?
var sat : hoursOfOperationData?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let sun = json["sun"]{
self.sun = hoursOfOperationData.init(sun)
}
if let sat = json["sat"]{
self.sat = hoursOfOperationData.init(sat)
}
if let fri = json["fri"]{
self.fri = hoursOfOperationData.init(fri)
}
if let thu = json["thu"] {
self.thu = hoursOfOperationData.init(thu)
}
if let wed = json["wed"]{
self.wed = hoursOfOperationData.init(wed)
}
if let tue = json["tue"]{
self.tue = hoursOfOperationData.init(tue)
}
if let mon = json["mon"] {
self.mon = hoursOfOperationData.init(mon)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
//hoursOfOperationData
struct hoursOfOperationData{
var enabled : AnyObject?
var schedule : [scheduleData]?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let enabled = json["enabled"]{
self.enabled = enabled as AnyObject
}
if let schedule = json["schedule"] as? NSArray{
for dic in schedule{
schedule.adding(scheduleData.init(dic as AnyObject))
}
self.schedule = schedule as? [scheduleData]
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
//scheduleData
struct scheduleData{
var time : scheduleDataForLocation?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let time = json["time"]{
self.time = scheduleDataForLocation.init(time)
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
//scheduleDataForLocation
struct scheduleDataForLocation{
var openTime : AnyObject?
var closeTime : AnyObject?
init(_ info: AnyObject) {
let s = String(describing: info)
let data = s.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
if let open = json["open"]{
self.openTime = open as AnyObject
}
if let close = json["close"]{
self.closeTime = close as AnyObject
}
} catch let error as NSError {
print("Failed to load: \(error.localizedDescription)")
}
}
}
While Preparing this model Im unable to parse the json and getting error message
' Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 6." UserInfo={NSDebugDescription=No string key for value in object around character 6.} '
Please suggest a correct way to handle the same. I have visited many stack overflow questions and answers but none answers my question. Any help would be appreciable.
You can directly use below model class, reference : http://www.jsoncafe.com/
class OperationModel : NSObject, NSCoding{
var hoursOfOperation : HoursOfOperation!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
if let hoursOfOperationData = dictionary["hoursOfOperation"] as? [String:Any]{
hoursOfOperation = HoursOfOperation(fromDictionary: hoursOfOperationData)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if hoursOfOperation != nil{
dictionary["hoursOfOperation"] = hoursOfOperation.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
hoursOfOperation = aDecoder.decodeObject(forKey: "hoursOfOperation") as? HoursOfOperation
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
#objc func encode(with aCoder: NSCoder)
{
if hoursOfOperation != nil{
aCoder.encode(hoursOfOperation, forKey: "hoursOfOperation")
}
}
}
class Schedule : NSObject, NSCoding{
var time : Time!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
if let timeData = dictionary["time"] as? [String:Any]{
time = Time(fromDictionary: timeData)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if time != nil{
dictionary["time"] = time.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
time = aDecoder.decodeObject(forKey: "time") as? Time
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
#objc func encode(with aCoder: NSCoder)
{
if time != nil{
aCoder.encode(time, forKey: "time")
}
}
}
class HoursOfOperation : NSObject, NSCoding{
var fri : Day!
var mon : Day!
var sat : Day!
var sun : Day!
var thu : Day!
var tue : Day!
var wed : Day!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
if let friData = dictionary["fri"] as? [String:Any]{
fri = Day(fromDictionary: friData)
}
if let monData = dictionary["mon"] as? [String:Any]{
mon = Day(fromDictionary: monData)
}
if let satData = dictionary["sat"] as? [String:Any]{
sat = Day(fromDictionary: satData)
}
if let sunData = dictionary["sun"] as? [String:Any]{
sun = Day(fromDictionary: sunData)
}
if let thuData = dictionary["thu"] as? [String:Any]{
thu = Day(fromDictionary: thuData)
}
if let tueData = dictionary["tue"] as? [String:Any]{
tue = Day(fromDictionary: tueData)
}
if let wedData = dictionary["wed"] as? [String:Any]{
wed = Day(fromDictionary: wedData)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if fri != nil{
dictionary["fri"] = fri.toDictionary()
}
if mon != nil{
dictionary["mon"] = mon.toDictionary()
}
if sat != nil{
dictionary["sat"] = sat.toDictionary()
}
if sun != nil{
dictionary["sun"] = sun.toDictionary()
}
if thu != nil{
dictionary["thu"] = thu.toDictionary()
}
if tue != nil{
dictionary["tue"] = tue.toDictionary()
}
if wed != nil{
dictionary["wed"] = wed.toDictionary()
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
fri = aDecoder.decodeObject(forKey: "fri") as? Day
mon = aDecoder.decodeObject(forKey: "mon") as? Day
sat = aDecoder.decodeObject(forKey: "sat") as? Day
sun = aDecoder.decodeObject(forKey: "sun") as? Day
thu = aDecoder.decodeObject(forKey: "thu") as? Day
tue = aDecoder.decodeObject(forKey: "tue") as? Day
wed = aDecoder.decodeObject(forKey: "wed") as? Day
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
#objc func encode(with aCoder: NSCoder)
{
if fri != nil{
aCoder.encode(fri, forKey: "fri")
}
if mon != nil{
aCoder.encode(mon, forKey: "mon")
}
if sat != nil{
aCoder.encode(sat, forKey: "sat")
}
if sun != nil{
aCoder.encode(sun, forKey: "sun")
}
if thu != nil{
aCoder.encode(thu, forKey: "thu")
}
if tue != nil{
aCoder.encode(tue, forKey: "tue")
}
if wed != nil{
aCoder.encode(wed, forKey: "wed")
}
}
}
class Time : NSObject, NSCoding{
var close : Int!
var open : Int!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
close = dictionary["close"] as? Int
open = dictionary["open"] as? Int
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if close != nil{
dictionary["close"] = close
}
if open != nil{
dictionary["open"] = open
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
close = aDecoder.decodeObject(forKey: "close") as? Int
open = aDecoder.decodeObject(forKey: "open") as? Int
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
#objc func encode(with aCoder: NSCoder)
{
if close != nil{
aCoder.encode(close, forKey: "close")
}
if open != nil{
aCoder.encode(open, forKey: "open")
}
}
}
class Day : NSObject, NSCoding{
var enabled : Bool!
var schedule : [Schedule]!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
enabled = dictionary["enabled"] as? Bool
schedule = [Schedule]()
if let scheduleArray = dictionary["schedule"] as? [[String:Any]]{
for dic in scheduleArray{
let value = Schedule(fromDictionary: dic)
schedule.append(value)
}
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if enabled != nil{
dictionary["enabled"] = enabled
}
if schedule != nil{
var dictionaryElements = [[String:Any]]()
for scheduleElement in schedule {
dictionaryElements.append(scheduleElement.toDictionary())
}
dictionary["schedule"] = dictionaryElements
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
enabled = aDecoder.decodeObject(forKey: "enabled") as? Bool
schedule = aDecoder.decodeObject(forKey: "schedule") as? [Schedule]
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
#objc func encode(with aCoder: NSCoder)
{
if enabled != nil{
aCoder.encode(enabled, forKey: "enabled")
}
if schedule != nil{
aCoder.encode(schedule, forKey: "schedule")
}
}
}
Use :
let jsonData = try Data(contentsOf: url)
let json = try JSONSerialization.jsonObject(with: jsonData) as! [String : Any]
let arrOper = OperationModel(fromDictionary: json)
Output:
I have found some problem if you JSON which is related with Missing opening brace or square bracket { }
so first correct that and then your JSON will be:
{
"hoursOfOperation" : {
"sat" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1020,
"open" : 600
}
}
]
},
"fri" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"sun" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1020,
"open" : 600
}
}
]
},
"mon" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 960
}
}
]
},
"tue" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"wed" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
},
"thu" : {
"enabled" : true,
"schedule" : [
{
"time" : {
"close" : 1260,
"open" : 660
}
}
]
}
}
}
Now you can create your Codable protocol with THIS site and code it will look like:
import Foundation
struct ScheduleData: Codable {
let hoursOfOperation: HoursOfOperation
}
struct HoursOfOperation: Codable {
let sat, fri, sun, mon: Fri
let tue, wed, thu: Fri
}
struct Fri: Codable {
let enabled: Bool
let schedule: [Schedule]
}
struct Schedule: Codable {
let time: Time
}
struct Time: Codable {
let close, timeOpen: Int
enum CodingKeys: String, CodingKey {
case close
case timeOpen = "open"
}
}
Uses :
let scheduleData = try? JSONDecoder().decode(ScheduleData.self, from: response.data!)
let close_time = scheduleData?.hoursOfOperation.sat.schedule[0].time.close
Print(close_time)
Output :
1020
Related
I am trying to pass data into variable by displaying it(NameList and BasketDetail). But the basket data is empty(BasketDetail). How can i solve this (Output:Retrieve data: [“name”: Mike, “basket”: <__NSArray0 0x28112c060()])?
Firebase node;
"Pro_" : {
"-MsVcfY7pZI74r0E1ULD" : {
"basket" : {
"-Mruf-UdXxpLK0l8Qgw4" : {
"cat" : "Tech",
"info" : "iOS",
"orderid" : "Ref_1",
},
"-MszuLj3cxm_gE9m-VPO" : {
"cat" : "Tech",
"info" : "Android",
"orderid" : "Ref_2",
}
},
"name" : "-Mike",
}
}
My function;
private func populateNameLists() {
ref = Database.database().reference()
self.ref.child(“Pro_”).observeSingleEvent(of:.value) { (snapshot) in
self.nameLists.removeAll()
let nameListDictionary = snapshot.value as? [String:Any] ?? [:]
for (key,_) in nameListDictionary {
if let nameListDictionary = nameListDictionary[key] as? [String:Any] {
if let nameList = NameList(nameListDictionary) {
self.nameLists.append(nameList)
print(“Retrieve data:”, try! nameList.toDictionary())
}}}}}
Output(Basket's empty ???)
Retrieve data: [“name”: Mike, “basket”: <__NSArray0 0x28112c060()]
Name List
typealias JSONDictionary = [String:Any]
class NameList : Codable {
var basket :[BasketDetail] = [BasketDetail]()
var name: String!
init(name:String ) {
self.name = name
}
init?(_ dictionary :[String:Any]) {
guard let name = dictionary[“name”] as? String else {
return nil
self.name = name
let basketItemsDictionary = dictionary[“basket”] as? [JSONDictionary]
if let dictionaries = basketItemsDictionary {
self.basket = dictionaries.compactMap(BasketDetail.init)
}}}
Basket Detail
class BasketDetail : Codable{
var cat : String
var info : String
var orderid : String
init(cat :String, info :String, orderid :String) {
self.cat = cat
self.info = info
self.orderid = orderid
}
init?(dictionary :JSONDictionary) {
guard let cat = dictionary["cat"] as? String else {
return nil
}
guard let info = dictionary["info"] as? String else {
return nil
}
guard let orderid = dictionary["orderid"] as? String else {
return nil
}
self.cat = cat
self.info = info
self.orderid = orderid
}}
My API response is look like this
{
"error": false,
"id": "6",
"user_id": 7,
"users": [
{
"user_id": 1,
"username": "spiderman"
},
{
"user_id": 7,
"username": "wonderwoman"
}
],
"info": [
{
"id": 471,
"message": "abc",
"age": 10,
}
]
}
I know how to initialize the value of id,user_id and error in NSOject. But I dont know how can I initialize the array of users and info in the same NSObject class.
Now I initialize the JSON like this
import UIKit
import SwiftyJSON
class MyItem: NSObject {
var userId : Int
var error : Bool
var id : Int
init?(dict: [String :JSON]) {
self.id = dict["id"]?.int ?? 0
self.error = dict["error"]?.bool ?? false
self.userId = dict["userId"]?.int ?? 0
}
}
Now the problem is I don't know how to initialize data inside the users and info dictionary.How should I arrange it and how can I use it in other class
Kindly give an example.
Use as below,
Root Class :-
import Foundation
import SwiftyJSON
class RootClass : NSObject, NSCoding{
var error : Bool!
var id : String!
var info : [Info]!
var userId : Int!
var users : [User]!
/**
* Instantiate the instance using the passed json values to set the properties values
*/
init(fromJson json: JSON!){
if json.isEmpty{
return
}
error = json["error"].boolValue
id = json["id"].stringValue
info = [Info]()
let infoArray = json["info"].arrayValue
for infoJson in infoArray{
let value = Info(fromJson: infoJson)
info.append(value)
}
userId = json["user_id"].intValue
users = [User]()
let usersArray = json["users"].arrayValue
for usersJson in usersArray{
let value = User(fromJson: usersJson)
users.append(value)
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
let dictionary = [String:Any]()
if error != nil{
dictionary["error"] = error
}
if id != nil{
dictionary["id"] = id
}
if info != nil{
var dictionaryElements = [[String:Any]]()
for infoElement in info {
dictionaryElements.append(infoElement.toDictionary())
}
dictionary["info"] = dictionaryElements
}
if userId != nil{
dictionary["user_id"] = userId
}
if users != nil{
var dictionaryElements = [[String:Any]]()
for usersElement in users {
dictionaryElements.append(usersElement.toDictionary())
}
dictionary["users"] = dictionaryElements
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
error = aDecoder.decodeObject(forKey: "error") as? Bool
id = aDecoder.decodeObject(forKey: "id") as? String
info = aDecoder.decodeObject(forKey: "info") as? [Info]
userId = aDecoder.decodeObject(forKey: "user_id") as? Int
users = aDecoder.decodeObject(forKey: "users") as? [User]
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
func encode(with aCoder: NSCoder)
{
if error != nil{
aCoder.encode(error, forKey: "error")
}
if id != nil{
aCoder.encode(id, forKey: "id")
}
if info != nil{
aCoder.encode(info, forKey: "info")
}
if userId != nil{
aCoder.encode(userId, forKey: "user_id")
}
if users != nil{
aCoder.encode(users, forKey: "users")
}
}
}
User Class :-
import Foundation
import SwiftyJSON
class User : NSObject, NSCoding{
var userId : Int!
var username : String!
/**
* Instantiate the instance using the passed json values to set the properties values
*/
init(fromJson json: JSON!){
if json.isEmpty{
return
}
userId = json["user_id"].intValue
username = json["username"].stringValue
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
let dictionary = [String:Any]()
if userId != nil{
dictionary["user_id"] = userId
}
if username != nil{
dictionary["username"] = username
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
userId = aDecoder.decodeObject(forKey: "user_id") as? Int
username = aDecoder.decodeObject(forKey: "username") as? String
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
func encode(with aCoder: NSCoder)
{
if userId != nil{
aCoder.encode(userId, forKey: "user_id")
}
if username != nil{
aCoder.encode(username, forKey: "username")
}
}
}
Info Class :-
import Foundation
import SwiftyJSON
class Info : NSObject, NSCoding{
var age : Int!
var id : Int!
var message : String!
/**
* Instantiate the instance using the passed json values to set the properties values
*/
init(fromJson json: JSON!){
if json.isEmpty{
return
}
age = json["age"].intValue
id = json["id"].intValue
message = json["message"].stringValue
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
let dictionary = [String:Any]()
if age != nil{
dictionary["age"] = age
}
if id != nil{
dictionary["id"] = id
}
if message != nil{
dictionary["message"] = message
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
age = aDecoder.decodeObject(forKey: "age") as? Int
id = aDecoder.decodeObject(forKey: "id") as? Int
message = aDecoder.decodeObject(forKey: "message") as? String
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
func encode(with aCoder: NSCoder)
{
if age != nil{
aCoder.encode(age, forKey: "age")
}
if id != nil{
aCoder.encode(id, forKey: "id")
}
if message != nil{
aCoder.encode(message, forKey: "message")
}
}
}
The best way to do this is to create 2 different classes for user and info as follows:
class MyItem : NSObject {
var error : Bool!
var id : String!
var info : [Info]!
var userId : Int!
var users : [User]!
init(fromJson json: JSON!){
if json == nil{
return
}
error = json["error"].boolValue
id = json["id"].stringValue
info = [Info]()
let infoArray = json["info"].arrayValue
for infoJson in infoArray{
let value = Info(fromJson: infoJson)
info.append(value)
}
userId = json["user_id"].intValue
users = [User]()
let usersArray = json["users"].arrayValue
for usersJson in usersArray{
let value = User(fromJson: usersJson)
users.append(value)
}
}
}
class User : NSObject {
var userId : Int!
var username : String!
init(fromJson json: JSON!){
if json == nil{
return
}
userId = json["user_id"].intValue
username = json["username"].stringValue
}
}
class Info : NSObject {
var age : Int!
var id : Int!
var message : String!
init(fromJson json: JSON!){
if json == nil{
return
}
age = json["age"].intValue
id = json["id"].intValue
message = json["message"].stringValue
}
}
By doing this you would be able to directly access the value of user and info like for eg: MyItem.users[index].userId
Do Like this,
class MyItem: NSObject {
var userId : Int
var error : Bool
var id : Int
var users : [[String:Any]] = []
var info : [[String:Any]] = []
init?(dict: [String :JSON]) {
self.id = dict["id"]?.int ?? 0
self.error = dict["error"]?.bool ?? false
self.userId = dict["userId"]?.int ?? 0
self.users = dict["users"] ?? []
self.info = dict["info"] ?? []
}
}
No offense to the developers of SwiftyJSON, it is a great library, but in Swift 4 to decode JSON SwiftyJSON became obsolete.
With the Decodable protocol you are able to decode the JSON without any extra code.
Create one struct including the two substructs, the coding keys are only necessary to map snake_case to camelCase.
struct MyItem: Decodable {
private enum CodingKeys: String, CodingKey { case userId = "user_id", error, id, users, info}
let userId : Int
let error : Bool
let id : String
let users : [User]
let info : [Info]
struct User : Decodable {
private enum CodingKeys: String, CodingKey { case userId = "user_id", username}
let userId : Int
let username : String
}
struct Info : Decodable {
let message : String
let id, age : Int
}
}
Now decode the JSON
let jsonString = """
{
"error": false,
"id": "6",
"user_id": 7,
"users": [{"user_id": 1, "username": "spiderman"},{"user_id": 7,"username": "wonderwoman"}],
"info": [{"id": 471,"message": "abc", "age": 10}]
}
"""
do {
let data = Data(jsonString.utf8)
let decoder = JSONDecoder()
let result = try JSONDecoder().decode(MyItem.self, from: data)
for user in result.users {
print(user.userId, user.username)
}
} catch { print(error) }
I have searched a lot to correctly parse a multi dimensional JSON arrays in swift. From the search results , what i could grasp was that it some what similar to retro-fit parsing in Android. i.e creating a parsing class for each json response. Please excuse me if i am making a mistake. I am new to IOS swif.
This is the distance matrix api json output for my distance call
{
"destination_addresses" : [ "My Destination" ],
"origin_addresses" : [
"My Source"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "23.3 km",
"value" : 23333 //lastly i take this
},
"duration" : {
"text" : "49 mins",
"value" : 2938
},
"status" : "OK" //then i check this
}
]
}
],
"status" : "OK" //first i check this
}
This how i did it
This is my api call(works fine)
http://maps.googleapis.com/maps/api/distancematrix/json?origins=latitude,longitude&destinations=latitude,longitude&mode=driving&language=en-EN&sensor=false
func extract_Validate(jsonData:NSData)
{
var distInMeters = 0
var chkVarOne = "notOK"
let json1 = NSString(data: jsonData as Data, encoding: String.Encoding.utf8.rawValue)
let data = json1!.data(using: String.Encoding.utf8.rawValue, allowLossyConversion: false)
print(data!)
do{
print("Inside do")
let json = try JSONSerialization.jsonObject(with: jsonData as Data, options:.allowFragments) as! NSObject
if let dictionary = json as? [String: Any] {
print("inside dictionary ")
if let detailsDict = dictionary as? NSDictionary {
print("Parse Data")
for (key, value) in detailsDict {
print("Property: \"\(key as! String)\"")
if key as! String == "status" && value as! String == "OK"{
chkVarOne = "OK"
}
if chkVarOne == "OK"
{
if key as! String == "rows"
{
if let elementsDict = value as? NSDictionary {
for(keyEle, valueEle) in elementsDict{
}
}
if let elementsDict = value as? NSArray
{
if let array = elementsDict as? [Any] {
if let firstObject = array.first {
if let distanceSet = firstObject as? NSDictionary{
for(keyDis, valueDis) in distanceSet{
// print("Property: \"\(keyDis as! String)\"")
if keyDis as! String == "elements"
{
if let DistDict = valueDis as? NSDictionary {
for(keyDict, valueDict) in DistDict{
print("Property: \"\(keyDict as! String)\"")
}
}
if let DistDict = valueDis as? NSArray {
// print(DistDict)
if let distArr = DistDict as?[Any]{
if let frst = distArr.first{
//print(frst)
if let distOne = frst as? NSDictionary{
var checkvar = "notOK"
for(ketOneDis, valOneDis) in distOne{
// print("Property: \"\(ketOneDis as! String)\"", valOneDis)
if ketOneDis as! String == "status" && valOneDis as! String == "OK"{
checkvar = "OK"
}
if checkvar == "OK"
{
if ketOneDis as! String == "distance"
{
// print(valOneDis)
if let valtwoDis = valOneDis as? NSDictionary{
for(kDis, vDis) in valtwoDis{
// print("Property: \"\(kDis as! String)\"", vDis)
if kDis as! String == "value"{
distInMeters = vDis as! Int
}
}
if let valTwoDis = valOneDis as? NSArray{
print(valTwoDis)
}
}
}
}
}
}
}
}
}
}
//print(distanceSet,"rttgtgtgtgtgtgtg")
}
if let distSet = firstObject as? NSArray{
print(distSet,"dfdffddfdfdfd")
}
}
}
}
}
//let rows
}
}
} //ending here
}
}
}
catch{
print("error in JSONSerialization")
}
print(distInMeters," is the resulting value")
}
This code is working fine fine. But i know this is not the way to do it.
So please help me
I think there might be some errors occur in this code later. Don't know for sure
The easiest and most effective way to do this is by using object mapping. Something like Gloss (https://github.com/hkellaway/Gloss) would do the trick. In your case you will have the following classes (objects):
Response
import Foundation
import Gloss
//MARK: - Response
public struct Response: Glossy {
public let destinationAddresses : [String]!
public let originAddresses : [String]!
public let rows : [Row]!
public let status : String!
//MARK: Decodable
public init?(json: JSON){
destinationAddresses = "destination_addresses" <~~ json
originAddresses = "origin_addresses" <~~ json
rows = "rows" <~~ json
status = "status" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"destination_addresses" ~~> destinationAddresses,
"origin_addresses" ~~> originAddresses,
"rows" ~~> rows,
"status" ~~> status,
])
}
}
Element:
import Foundation
import Gloss
//MARK: - Element
public struct Element: Glossy {
public let distance : Distance!
public let duration : Distance!
public let status : String!
//MARK: Decodable
public init?(json: JSON){
distance = "distance" <~~ json
duration = "duration" <~~ json
status = "status" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"distance" ~~> distance,
"duration" ~~> duration,
"status" ~~> status,
])
}
}
Row:
import Foundation
import Gloss
//MARK: - Row
public struct Row: Glossy {
public let elements : [Element]!
//MARK: Decodable
public init?(json: JSON){
elements = "elements" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"elements" ~~> elements,
])
}
}
Distance:
import Foundation
import Gloss
//MARK: - Distance
public struct Distance: Glossy {
public let text : String!
public let value : Int!
//MARK: Decodable
public init?(json: JSON){
text = "text" <~~ json
value = "value" <~~ json
}
//MARK: Encodable
public func toJSON() -> JSON? {
return jsonify([
"text" ~~> text,
"value" ~~> value,
])
}
}
After creating the classes, you map the JSON to the Object by doing the following:
let jsonResponse = ..//Your JSON response
guard let distanceResponse = Response(json: jsonResponse) else {
// handle decoding failure here
}
//You can use the distanceResponse object here as an object and access it's values.
Learn more about object mapping in this tutorial: https://www.raywenderlich.com/150322/swift-json-tutorial-2
Hi i am beginner for swift ios and my requirement is have to display Json response to table list i got response from web-services and response seems like below
MY requirement is how to map that model classes to Array and how to display them in tableList can some one help me please
JsonResponse:-
[{
"_id" : "5470def9e0c0be27780121d7",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/5470def9e0c0be27780121d7_180.png",
"name" : "Mondo",
"hasVip" : false,
"location" : {
"city" : "Madrid"
}
}, {
"_id" : "540b2ff281b30f3504a1c72f",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540b2ff281b30f3504a1c72f_180.png",
"name" : "Teatro Kapital",
"hasVippler" : false,
"location" : {
"address" : "Atocha, 125",
"city" : "Madrid"
}
}, {
"_id" : "540cd44581b30f3504a1c73b",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540cd44581b30f3504a1c73b_180.png",
"name" : "Charada",
"hasVippler" : false,
"location" : {
"address" : "La Bola, 13",
"city" : "Madrid"
}
}]
mapping:
Club:-
class Club {
var id: String = ""
var name: String = ""
var imageUrl: String = ""
var hasVip: Bool = false
var desc: String = ""
var location: [Location] = []
}
Location:-
class Location {
var country: String = ""
var city: String = ""
var address: String = ""
var zip: String = ""
var underground: [String] = []
}
NSURlSession code:-
class BackGroundPostCall: NSObject {
var delegate:PostProtocol?
func callPostService(url:String,params:NSDictionary){
print("url is===>\(url)")
let request = NSMutableURLRequest(URL: NSURL(string:url)!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
//Note : Add the corresponding "Content-Type" and "Accept" header. In this example I had used the application/json.
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(params, options: [])
let task = session.dataTaskWithRequest(request) { data, response, error in
guard data != nil else {
print("no data found: \(error)")
return
}
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
print("Response: \(json)")
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)// No error thrown, but not NSDictionary
print("Error could not parse JSON: \(jsonStr)")
}
} catch let parseError {
print(parseError)// Log the error thrown by `JSONObjectWithData`
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
}
task.resume()
}
}
For mapping you can use Alamofire's extension ObjectMapper.
Ex:
[{
"_id" : "5470def9e0c0be27780121d7",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/5470def9e0c0be27780121d7_180.png",
"name" : "Mondo",
"hasVip" : false,
"location" : {
"city" : "Madrid"
}
}, {
"_id" : "540b2ff281b30f3504a1c72f",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540b2ff281b30f3504a1c72f_180.png",
"name" : "Teatro Kapital",
"hasVippler" : false,
"location" : {
"address" : "Atocha, 125",
"city" : "Madrid"
}
}, {
"_id" : "540cd44581b30f3504a1c73b",
"imageUrl" : "https:\/\/s3-eu-west-1.amazonaws.com\/api-static\/clubs\/540cd44581b30f3504a1c73b_180.png",
"name" : "Charada",
"hasVippler" : false,
"location" : {
"address" : "La Bola, 13",
"city" : "Madrid"
}
}]
And mapper class:
import ObjectMapper
class Location: Mappable {
var address: String?
var city: String?
required init?(map: Map){
}
func mapping(map: Map) {
address <- map["address"]
city <- map["city"]
}
}
class Club: Mappable {
var id: String?
var imageUrl: Int?
var name: String?
var hasVip: Bool = false
var location: Location?
required init?(map: Map){
}
func mapping(map: Map) {
id <- map["_id"]
imageUrl <- map["imageUrl"]
name <- map["name"]
hasVip <- map["hasVippler"]
location <- map["location"]
}
}
And this way very flexible and transparent to use.
https://github.com/Alamofire/Alamofire
https://github.com/tristanhimmelman/AlamofireObjectMapper
Using example:
Alamofire.request(URL).responseArray { (response: DataResponse<[Club]>) in
let clubs = response.result.value
if let clubs = clubs {
for club in clubs {
print(club.name)
print(club.location.city)
}
}
}
You can make model class using this url : http://www.jsoncafe.com/
open this link and put your json in JSON tab and select any Code Template that is you want. and there is you can also add prefix class name and root class name if you want other wise it is optional. and last click on generate button, your json class is ready.!!
Step1: Create your model like below.
class Club {
var id: String = ""
var name: String = ""
var imageUrl: String = ""
var hasVip: Bool = false
var desc: String = ""
var location = Location()
init?(dictionary:[String:Any],location: Location) {
guard let id = dictionary["_id"],
let name = dictionary["name"],
let imageUrl = dictionary["imageUrl"],
let hasVip = dictionary["hasVippler"]
else {
return nil
}
self.id = id
self.name = name
self.imageUrl = imageUrl
self.hasVip = hasVip
self.location = location
}
}
}
class Location {
var country: String = ""
var city: String = ""
var address: String = ""
var zip: String = ""
init?(dictionary:[String:Any]) {
guard let country = dictionary["country"],
let city = dictionary["city"],
let address = dictionary["address"],
let zip = dictionary["zip"]
else {
return nil
}
self.country = country
self.city = city
self.address = address
self.zip = zip
}
}
}
Step2: Declare your array as below.
var myTargetArray = [Club]()
Step3: Json Parsing.
do {
if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
print("Response: \(json)")
for club in json {
if let clubDictionary = club as? NSDictionary {
if let locationDict = clubDictionary["location"] as? NSDictionary{
if let location = Location(dictionary: locationDict) {
if let clubObj = Club(dictionary: clubDictionary, location:location) {
myTargetArray.append(clubObj)
}
}
}
}
}
} else {
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)// No error thrown, but not NSDictionary
print("Error could not parse JSON: \(jsonStr)")
}
} catch let parseError {
print(parseError)// Log the error thrown by `JSONObjectWithData`
let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("Error could not parse JSON: '\(jsonStr)'")
}
Note: I removed underground array from location model since your json data does not contain it.
Finally you can use your target array as your tableview source. Happy coding...
Model class:
class test : Unboxable {
let id : String
let imageURl : String
let name : String
let hasVip : Bool
let city : String
required init(unboxer: Unboxer) throws {
self.id = unboxer.unbox(key: "id") ?? ""
self.imageURl = unboxer.unbox(key: "imageUrl") ?? ""
self.name = unboxer.unbox(key: "name") ?? ""
self.hasVip = unboxer.unbox(key: "hasVip") ?? false
self.city = (unboxer.unbox(key: "city") ?? nil)!
}
}
parse json:
if let object = json as? [Any] {
// json is an array
var data :[test] = []
for respObject in object {
var dict = respObject as? Dictionary<String,AnyObject>
dict?["city"] = dict?["location"]?["city"] as AnyObject
let result1: [test] = try unbox(dictionaries: [dict!])
data.append(contentsOf: result1)
}
print(data)
For more details follow
https://github.com/JohnSundell/Unbox
**Api call with model class, swiftyjson and alamofire,Pod install alamofire and swiftyjson libraries, create collection view cell class, Create a class and write the following code
**
import UIKit
import Alamofire
import SwiftyJSON
var myResponse : JSON? = nil
var users : [reasonList] = []
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
feedbackApi()
}
func feedbackApi(){
DispatchQueue.main.async {
let url = URL(string: "--------")
let urlRequest = URLRequest(url: url!)
Alamofire.request(urlRequest)
.responseJSON { response in
switch response.result{
case.success(let data):
print("dddd :",data)
self.myResponse = JSON(data)
print(self.myResponse as Any)
let a = self.myResponse![0]["reasonList"]
print(a)
for i in 0..<a.count{
let single = reasonList(reasonListJson: a[i])
self.users.append(single)
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
case .failure(let error):
print("dddd",error)
}
}
}
}
}
create a model class
import Foundation
import SwiftyJSON
class user{
var deviceId = String()
var deviceName = String()
var deviceLocationId = Int()
var locationName = String()
var welcomeText = String()
var reason=[reasonList]()
init(userJson:JSON) {
self.deviceId = userJson["deviceId"].stringValue
self.deviceName = userJson["deviceName"].stringValue
self.deviceLocationId = userJson["deviceLocationId"].intValue
self.locationName = userJson["locationName"].stringValue
self.welcomeText = userJson["welcomeText"].stringValue
self.reason = [reasonList(reasonListJson: userJson["reason"])]
}}
class reasonList{
var reason = String()
var id = Int()
init(reasonListJson:JSON) {
self.reason = reasonListJson["reason"].stringValue
self.id = reasonListJson["id"].intValue
]}
**create a collection view in view **
import UIKit
import Alamofire
import SwiftyJSON
class ReasonViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
//DelayCall()
// Do any additional setup after loading the view.
}
func DelayCall() {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { // Change `2.0` to the desired number of seconds.
_ = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController")as! HomeViewController
self.navigationController?.popViewController(animated: true)
}
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return users.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReasonCollectionViewCell", for: indexPath) as! ReasonCollectionViewCell
let useee = users[indexPath.row]
print(useee)
cell.reasonLabel.text = useee.reason
return cell
}}
You can make model class using this url : https://www.json4swift.com/
Open this link and paste your JSON and select from below option you want. Click generate, it will generate class files you can download and used it in your project.
In my project, there is a bag object which was declared as static and when a customer click the "approve order" button, I serialize the bag object to JSON String. There is no problem so far. However, when I print the serialized json string, all of the "IDs" with Int in the bag converted to Bool type in json string result.
The related code blocks for this process as below:
This is my "Serialize" class:
public class Serializable : NSObject
{
func toDictionary() -> NSDictionary
{
var aClass : AnyClass? = self.dynamicType
var propertiesCount : CUnsignedInt = 0
var propertiesInAClass : UnsafeMutablePointer<objc_property_t> = class_copyPropertyList(aClass, &propertiesCount)
var propertiesDictionary : NSMutableDictionary = NSMutableDictionary()
for(var i = 0; i < Int(propertiesCount); i++)
{
var property = propertiesInAClass[i]
var propName = NSString(CString: property_getName(property), encoding: NSUTF8StringEncoding)
var propType = property_getAttributes(property)
var propValue : AnyObject! = self.valueForKey(propName!)
if(propValue is Serializable)
{
propertiesDictionary.setValue((propValue as Serializable).toDictionary(), forKey: propName!)
}
else if(propValue is Array<Serializable>)
{
var subArray = Array<NSDictionary>()
for item in (propValue as Array<Serializable>)
{
subArray.append(item.toDictionary())
}
propertiesDictionary.setValue(subArray, forKey: propName!)
}
else if(propValue is NSData)
{
propertiesDictionary.setValue((propValue as NSData).base64EncodedStringWithOptions(nil), forKey: propName!)
}
else if(propValue is Bool)
{
propertiesDictionary.setValue((propValue as Bool).boolValue, forKey: propName!)
}
else if(propValue is NSDate)
{
var date = propValue as NSDate
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "Z"
var dateString = NSString(format: "/Date(%.0f000%#)/", date.timeIntervalSince1970, dateFormatter.stringFromDate(date))
propertiesDictionary.setValue(dateString, forKey: propName!)
}
else
{
propertiesDictionary.setValue(propValue, forKey: propName!)
}
}
return propertiesDictionary
}
func toJson() -> NSData!
{
var dictionary = self.toDictionary()
var err: NSError?
return NSJSONSerialization.dataWithJSONObject(dictionary, options:NSJSONWritingOptions(0), error: &err)
}
func toJsonString() -> NSString!
{
return NSString(data: self.toJson(), encoding: NSUTF8StringEncoding)
}
override init()
{
}
}
This is my BagItem class:
class BagItem: Serializable, Hashable {
var uniqueID: Int = 0
override var hashValue: Int { return uniqueID.hashValue }
var bagItemId: String
var item: Item
var boughtDate: NSDate!
var boughtTime: String
var branch: Branch
var isMainItem: Bool
override init()
{
self.bagItemId = ""
self.item = Item()
self.boughtDate = NSDate()
self.boughtTime = ""
self.branch = Branch()
self.isMainItem = false
}
}
func ==(lhs: BagItem, rhs: BagItem) -> Bool
{
return lhs.uniqueID == rhs.uniqueID
}
This is my "SerializationBag" class:
class SerializableBag: Serializable
{
var bag: Array<BagItem> = []
override init()
{
}
}
This is my "ConvertBagToJSON" method in Bag class:
static func ConvertBagToJson() -> NSString
{
var serializer: SerializableBag = SerializableBag()
serializer.bag = self.bag
return serializer.toJsonString()
}
And my returned JSON String result as below:
{
"bag": [
{
"branch": {
"city": {
"cityId": false,
"cityName": ""
},
"town": {
"townName": "",
"townId": false
},
"branchName": "Branch",
"branchId": true,
"business": {
"businessPhotoPath": "",
"businessName": "",
"businessId": true
},
"branchAddress": "Some Address",
"branchTelephone": ""
},
"uniqueID": false,
"boughtDate": "/Date(1414581909000+0200)/",
"item": {
"itemName": "Pencil",
"itemId": true,
"itemPrice": true
},
"isMainItem": true,
"bagItemId": "9674D47B-0D2F-46CC-BA16-754875AE277D",
"hashValue": false,
"boughtTime": "00:30"
}
]
}
As you see, in JSON String, IDs are Bool, but they have to be in Int type. How can I solve this problem ?
Thank you for your answers
Best regards
This is because Int is bridged to NSNumber and NSNumber is always is Bool.
In your case, you don't need these lines:
else if(propValue is Bool)
{
propertiesDictionary.setValue((propValue as Bool).boolValue, forKey: propName!)
}
You can just delete them, because NSJSONSerialization can handle it.
let flg:NSNumber = true
let id:NSNumber = 1
let dict:NSDictionary = [
"bool": flg,
"id": id
]
let jsonDat = NSJSONSerialization.dataWithJSONObject(dict, options: .allZeros, error: nil)!
let jsonStr = NSString(data: dat, encoding:NSUTF8StringEncoding)
// -> {"id":1,"bool":true}
more relevant example:
class Foo:NSObject {
var flg:Bool = true
var id:Int = 1
}
let obj = Foo()
let dict:NSDictionary = [
"flg": obj.valueForKey("flg")!,
"id": obj.valueForKey("id")!
]
let jsonData = NSJSONSerialization.dataWithJSONObject(dict, options: .allZeros, error: nil)!
let jsonStr = NSString(data: jsonData, encoding:NSUTF8StringEncoding)!
// -> {"flg":true,"id":1}