Swift: display arrayobject to tableView












0














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

}
}
}









share|improve this question
























  • 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
















0














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

}
}
}









share|improve this question
























  • 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














0












0








0







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

}
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












2 Answers
2






active

oldest

votes


















0














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)





share|improve this answer























  • 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



















0














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.






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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)





    share|improve this answer























    • 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
















    0














    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)





    share|improve this answer























    • 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














    0












    0








    0






    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)





    share|improve this answer














    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)






    share|improve this answer














    share|improve this answer



    share|improve this answer








    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


















    • 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













    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0






        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 21 '18 at 11:22

























        answered Nov 21 '18 at 8:36









        Thomas G.

        590720




        590720






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga