Swift: display arrayobject to tableView
im trying to dispaly array object come from api response as [[String: Any]] at table view
and thats my struct
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: JSON]) {
guard let image = dict["main_department_image"]?.imagePath, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"]?.string)!
}
struct subData {
var dep: String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
}
}
}
ios swift uitableview
add a comment |
im trying to dispaly array object come from api response as [[String: Any]] at table view
and thats my struct
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: JSON]) {
guard let image = dict["main_department_image"]?.imagePath, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"]?.string)!
}
struct subData {
var dep: String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
}
}
}
ios swift uitableview
can you put your jason response here?
– Jatin Kathrotiya
Nov 21 '18 at 4:46
@JatinKathrotiya that is my response "sub_depart" : [ { "sub_depart" : [ { "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg" }, ],
– mahmoud AbdlAziz
Nov 21 '18 at 4:52
Please check my answer
– Jatin Kathrotiya
Nov 21 '18 at 5:12
what is the problem that you are facing?
– Arnab Hore
Nov 21 '18 at 6:15
add a comment |
im trying to dispaly array object come from api response as [[String: Any]] at table view
and thats my struct
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: JSON]) {
guard let image = dict["main_department_image"]?.imagePath, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"]?.string)!
}
struct subData {
var dep: String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
}
}
}
ios swift uitableview
im trying to dispaly array object come from api response as [[String: Any]] at table view
and thats my struct
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: JSON]) {
guard let image = dict["main_department_image"]?.imagePath, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"]?.string)!
}
struct subData {
var dep: String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
}
}
}
ios swift uitableview
ios swift uitableview
edited Nov 21 '18 at 6:16
Arnab Hore
2,06611128
2,06611128
asked Nov 21 '18 at 1:50
mahmoud AbdlAziz
84
84
can you put your jason response here?
– Jatin Kathrotiya
Nov 21 '18 at 4:46
@JatinKathrotiya that is my response "sub_depart" : [ { "sub_depart" : [ { "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg" }, ],
– mahmoud AbdlAziz
Nov 21 '18 at 4:52
Please check my answer
– Jatin Kathrotiya
Nov 21 '18 at 5:12
what is the problem that you are facing?
– Arnab Hore
Nov 21 '18 at 6:15
add a comment |
can you put your jason response here?
– Jatin Kathrotiya
Nov 21 '18 at 4:46
@JatinKathrotiya that is my response "sub_depart" : [ { "sub_depart" : [ { "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg" }, ],
– mahmoud AbdlAziz
Nov 21 '18 at 4:52
Please check my answer
– Jatin Kathrotiya
Nov 21 '18 at 5:12
what is the problem that you are facing?
– Arnab Hore
Nov 21 '18 at 6:15
can you put your jason response here?
– Jatin Kathrotiya
Nov 21 '18 at 4:46
can you put your jason response here?
– Jatin Kathrotiya
Nov 21 '18 at 4:46
@JatinKathrotiya that is my response "sub_depart" : [ { "sub_depart" : [ { "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg" }, ],
– mahmoud AbdlAziz
Nov 21 '18 at 4:52
@JatinKathrotiya that is my response "sub_depart" : [ { "sub_depart" : [ { "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg" }, ],
– mahmoud AbdlAziz
Nov 21 '18 at 4:52
Please check my answer
– Jatin Kathrotiya
Nov 21 '18 at 5:12
Please check my answer
– Jatin Kathrotiya
Nov 21 '18 at 5:12
what is the problem that you are facing?
– Arnab Hore
Nov 21 '18 at 6:15
what is the problem that you are facing?
– Arnab Hore
Nov 21 '18 at 6:15
add a comment |
2 Answers
2
active
oldest
votes
Please check below code to parse your json
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: Any]) {
guard let image = dict["main_department_image"] as? String, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"] as? String)!
subName =
for subDict in (dict["sub_depart"] as? [[String:Any]] ?? ){
subName.append(subData(dic: subDict))
}
}
}
struct subData {
var dep: String
var image :String
var id : String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
self.image = dic["sub_department_image"] as! String
self.id = dic["sub_department_id"] as! String
}
}
and if you want to access subdata struct out side of CategoriesDep class then declare structure outside CategoriesDep class
Parse your given json Respoise like
let json = [
[ "sub_depart" : [
[ "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg"
]
],
"main_department_id" : "2",
"main_department_name" : "main ",
"main_department_image" : "14.jpg"
],
]
var catDepart : [CategoriesDep] =
for dict in json {
catDepart.append(CategoriesDep(dict: dict)!)
}
print(catDepart[0].subName[0].dep)
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
|
show 1 more comment
You could use Codabel
protocol to be more swifty ;) and cleaning up the code.
let jsonString = "[{"sub_depart" : [ {"sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg"}]"
struct CategoriesDep: Codable {
let mainDepartmentName: String
let mainDepartmentImage: String
let mainDepartmentId: String
var subDepart: [SubData] =
}
struct SubData: Codable {
let subDepartmentName: String
let subDepartmentImage: String
let subDepartmentId: String
}
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var departments: [CategoriesDep]? = try? decoder.decode([CategoriesDep].self, from: jsonData)
...
}
Note the decoder.keyDecodingStrategy = .convertFromSnakeCase
here which is mapping the underscore (snake_case) API property names to your camelCase ones.
If you need different property names you have to implement CodingKeys
enum to map them.
For more detailed information check this link.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404232%2fswift-display-arrayobject-to-tableview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please check below code to parse your json
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: Any]) {
guard let image = dict["main_department_image"] as? String, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"] as? String)!
subName =
for subDict in (dict["sub_depart"] as? [[String:Any]] ?? ){
subName.append(subData(dic: subDict))
}
}
}
struct subData {
var dep: String
var image :String
var id : String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
self.image = dic["sub_department_image"] as! String
self.id = dic["sub_department_id"] as! String
}
}
and if you want to access subdata struct out side of CategoriesDep class then declare structure outside CategoriesDep class
Parse your given json Respoise like
let json = [
[ "sub_depart" : [
[ "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg"
]
],
"main_department_id" : "2",
"main_department_name" : "main ",
"main_department_image" : "14.jpg"
],
]
var catDepart : [CategoriesDep] =
for dict in json {
catDepart.append(CategoriesDep(dict: dict)!)
}
print(catDepart[0].subName[0].dep)
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
|
show 1 more comment
Please check below code to parse your json
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: Any]) {
guard let image = dict["main_department_image"] as? String, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"] as? String)!
subName =
for subDict in (dict["sub_depart"] as? [[String:Any]] ?? ){
subName.append(subData(dic: subDict))
}
}
}
struct subData {
var dep: String
var image :String
var id : String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
self.image = dic["sub_department_image"] as! String
self.id = dic["sub_department_id"] as! String
}
}
and if you want to access subdata struct out side of CategoriesDep class then declare structure outside CategoriesDep class
Parse your given json Respoise like
let json = [
[ "sub_depart" : [
[ "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg"
]
],
"main_department_id" : "2",
"main_department_name" : "main ",
"main_department_image" : "14.jpg"
],
]
var catDepart : [CategoriesDep] =
for dict in json {
catDepart.append(CategoriesDep(dict: dict)!)
}
print(catDepart[0].subName[0].dep)
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
|
show 1 more comment
Please check below code to parse your json
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: Any]) {
guard let image = dict["main_department_image"] as? String, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"] as? String)!
subName =
for subDict in (dict["sub_depart"] as? [[String:Any]] ?? ){
subName.append(subData(dic: subDict))
}
}
}
struct subData {
var dep: String
var image :String
var id : String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
self.image = dic["sub_department_image"] as! String
self.id = dic["sub_department_id"] as! String
}
}
and if you want to access subdata struct out side of CategoriesDep class then declare structure outside CategoriesDep class
Parse your given json Respoise like
let json = [
[ "sub_depart" : [
[ "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg"
]
],
"main_department_id" : "2",
"main_department_name" : "main ",
"main_department_image" : "14.jpg"
],
]
var catDepart : [CategoriesDep] =
for dict in json {
catDepart.append(CategoriesDep(dict: dict)!)
}
print(catDepart[0].subName[0].dep)
Please check below code to parse your json
class CategoriesDep: NSObject {
var depName: String
var depImage: String
var subName = [subData]()
init?(dict: [String: Any]) {
guard let image = dict["main_department_image"] as? String, !image.isEmpty else { return nil }
self.depImage = image
self.depName = (dict["main_department_name"] as? String)!
subName =
for subDict in (dict["sub_depart"] as? [[String:Any]] ?? ){
subName.append(subData(dic: subDict))
}
}
}
struct subData {
var dep: String
var image :String
var id : String
init(dic: [String: Any]) {
self.dep = dic["sub_department_name"] as! String
self.image = dic["sub_department_image"] as! String
self.id = dic["sub_department_id"] as! String
}
}
and if you want to access subdata struct out side of CategoriesDep class then declare structure outside CategoriesDep class
Parse your given json Respoise like
let json = [
[ "sub_depart" : [
[ "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg"
]
],
"main_department_id" : "2",
"main_department_name" : "main ",
"main_department_image" : "14.jpg"
],
]
var catDepart : [CategoriesDep] =
for dict in json {
catDepart.append(CategoriesDep(dict: dict)!)
}
print(catDepart[0].subName[0].dep)
edited Nov 21 '18 at 6:54
answered Nov 21 '18 at 5:11
Jatin Kathrotiya
389110
389110
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
|
show 1 more comment
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
i tried your answer.but unfortunately it didnt work.maybe i did something wrong..look i declare the struct in vc to var data = [categoriesDep.subData]() and when i try to display it print word dep
– mahmoud AbdlAziz
Nov 21 '18 at 5:34
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
you need to declare struct out side class
– Jatin Kathrotiya
Nov 21 '18 at 6:06
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
yes you need to put top of model class or any other place out side model class i updated my answer Please check
– Jatin Kathrotiya
Nov 21 '18 at 6:16
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
Still the same unfortunately.and there's two errors i should comment the depname and img bc i was cast res json but u changed to any and tried to fix but give me nil nut tried ur answer only without display those but still have the same issue that print word dep only
– mahmoud AbdlAziz
Nov 21 '18 at 6:29
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
I checked my answer and it run fine for me also i added answer how to parse your give json.
– Jatin Kathrotiya
Nov 21 '18 at 6:55
|
show 1 more comment
You could use Codabel
protocol to be more swifty ;) and cleaning up the code.
let jsonString = "[{"sub_depart" : [ {"sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg"}]"
struct CategoriesDep: Codable {
let mainDepartmentName: String
let mainDepartmentImage: String
let mainDepartmentId: String
var subDepart: [SubData] =
}
struct SubData: Codable {
let subDepartmentName: String
let subDepartmentImage: String
let subDepartmentId: String
}
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var departments: [CategoriesDep]? = try? decoder.decode([CategoriesDep].self, from: jsonData)
...
}
Note the decoder.keyDecodingStrategy = .convertFromSnakeCase
here which is mapping the underscore (snake_case) API property names to your camelCase ones.
If you need different property names you have to implement CodingKeys
enum to map them.
For more detailed information check this link.
add a comment |
You could use Codabel
protocol to be more swifty ;) and cleaning up the code.
let jsonString = "[{"sub_depart" : [ {"sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg"}]"
struct CategoriesDep: Codable {
let mainDepartmentName: String
let mainDepartmentImage: String
let mainDepartmentId: String
var subDepart: [SubData] =
}
struct SubData: Codable {
let subDepartmentName: String
let subDepartmentImage: String
let subDepartmentId: String
}
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var departments: [CategoriesDep]? = try? decoder.decode([CategoriesDep].self, from: jsonData)
...
}
Note the decoder.keyDecodingStrategy = .convertFromSnakeCase
here which is mapping the underscore (snake_case) API property names to your camelCase ones.
If you need different property names you have to implement CodingKeys
enum to map them.
For more detailed information check this link.
add a comment |
You could use Codabel
protocol to be more swifty ;) and cleaning up the code.
let jsonString = "[{"sub_depart" : [ {"sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg"}]"
struct CategoriesDep: Codable {
let mainDepartmentName: String
let mainDepartmentImage: String
let mainDepartmentId: String
var subDepart: [SubData] =
}
struct SubData: Codable {
let subDepartmentName: String
let subDepartmentImage: String
let subDepartmentId: String
}
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var departments: [CategoriesDep]? = try? decoder.decode([CategoriesDep].self, from: jsonData)
...
}
Note the decoder.keyDecodingStrategy = .convertFromSnakeCase
here which is mapping the underscore (snake_case) API property names to your camelCase ones.
If you need different property names you have to implement CodingKeys
enum to map them.
For more detailed information check this link.
You could use Codabel
protocol to be more swifty ;) and cleaning up the code.
let jsonString = "[{"sub_depart" : [ {"sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg"}]"
struct CategoriesDep: Codable {
let mainDepartmentName: String
let mainDepartmentImage: String
let mainDepartmentId: String
var subDepart: [SubData] =
}
struct SubData: Codable {
let subDepartmentName: String
let subDepartmentImage: String
let subDepartmentId: String
}
if let jsonData = jsonString.data(using: .utf8) {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
var departments: [CategoriesDep]? = try? decoder.decode([CategoriesDep].self, from: jsonData)
...
}
Note the decoder.keyDecodingStrategy = .convertFromSnakeCase
here which is mapping the underscore (snake_case) API property names to your camelCase ones.
If you need different property names you have to implement CodingKeys
enum to map them.
For more detailed information check this link.
edited Nov 21 '18 at 11:22
answered Nov 21 '18 at 8:36
Thomas G.
590720
590720
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404232%2fswift-display-arrayobject-to-tableview%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
can you put your jason response here?
– Jatin Kathrotiya
Nov 21 '18 at 4:46
@JatinKathrotiya that is my response "sub_depart" : [ { "sub_depart" : [ { "sub_department_name" : "hos", "sub_department_id" : "6", "sub_department_image" : "23.jpg" } ], "main_department_id" : "2", "main_department_name" : "main ", "main_department_image" : "14.jpg" }, ],
– mahmoud AbdlAziz
Nov 21 '18 at 4:52
Please check my answer
– Jatin Kathrotiya
Nov 21 '18 at 5:12
what is the problem that you are facing?
– Arnab Hore
Nov 21 '18 at 6:15