Swift 5 如何获取JSON多层数据追加?

Swift 5 How to get JSON multilayer data to append?

请帮助我,我正在尝试获取数组中的数组。将其应用于自定义 table 视图,其中包含 headers 和部分,headers 是第一个数组,部分是第二个。

我在行中添加了注释:print(subData.name),想不通,如何在数组中获取这个数组。

我还尝试在 table 视图中使用此 JSON 数据 headers 和部分。

感谢您提前帮助我,感谢您的宝贵时间。

Swift代码:

struct getSubData: Decodable {
    let id: Int
    let name: String
}

struct Section: Decodable {
    let id: Int
    let name: String
    let subData: [getSubData]
}

class MobileBrand {
    var brandName: String?
    var modelName: [String]?
    init(brandName: String, modelName: [String]) {
        self.brandName = brandName
        self.modelName = modelName
    }
}

class indexViewController: UIViewController {
    @IBOutlet weak var tableView: UITableView!
    var mobileBrand = [MobileBrand]()
    override func viewDidLoad() {
        super.viewDidLoad()
        let url = "https://nabulsi.com/nabulsi_app/main_sections_v4.json"
                             let urlObj = URL(string: url)
                             URLSession.shared.dataTask(with: urlObj!){(data, response, error) in
                                 do {
                                     let sections = try JSONDecoder().decode([Section].self, from: data!)
                                     //print(sections[0].subData[0].name)
                                     //print(section.subData)
                                     //print(sections.count)
                                     //print(sections[0].subData[0])
                                     for section in sections {
                                         print(section.name)
                                         print(subData.name)  //couldn't figure this out
                                     }
                                 } catch {
                                     print("We got an error")
                                 }
                             }.resume()


        // How to change the data below with JSON above.

        mobileBrand.append(MobileBrand.init(brandName: "Apple", modelName: ["iPhone 5s","iPhone 6","iPhone 6s", "iPhone 7+", "iPhone 8"]))
        mobileBrand.append(MobileBrand.init(brandName: "Samsung", modelName: ["Samsung M Series", "Samsung Galaxy Note 9", "Samsung Galaxy Note 9+"]))
        mobileBrand.append(MobileBrand.init(brandName: "Mi", modelName: ["Mi Note 7", "Mi Note 7 Pro", "Mi K20"]))
        mobileBrand.append(MobileBrand.init(brandName: "Huawei", modelName: ["Huawei Mate 20", "Huawei P30 Pro"]))
    }
}

JSON数据:

[
  {
         "name": "MAIN NAME 1 v4",
         "id": 1,
         "subData": [
             {
               "name": "1 NAME 1",
               "id": 101,
             },
             {
               "name": "1 NAME 2",
               "id": 102,
             },
             {
               "name": "1 NAME 3",
               "id": 103,
             },
             {
               "name": "1 NAME 4",
               "id": 104,
             }

         ],

     },


     {
            "name": "MAIN NAME 2 v4",
            "id": 2,
            "subData": [
                {
                  "name": "2 NAME 1",
                  "id": 201,
                },
                {
                  "name": "2 NAME 2",
                  "id": 202,
                },
                {
                  "name": "2 NAME 3",
                  "id": 203,
                }
            ],

        },

        {
               "name": "MAIN NAME 3 v4",
               "id": 3,
               "subData": [
                   {
                     "name": "3 NAME 1",
                     "id": 301,
                   },
                   {
                     "name": "3 NAME 2",
                     "id": 302,
                   },
                   {
                     "name": "3 NAME 3",
                     "id": 303,
                   }
               ],

           },

  ]

你有两个(嵌套)数组,所以你需要两个(嵌套)循环

for section in sections {
    print(section.name)
    for data in section.subData {
        print(data.name)
    }
}

在 table 视图中,外部数组表示部分,内部数组表示行。

MobileBrand 与 JSON 数据无关。