//CheckList.json { "CheckList": [ { "CheckItem": "施工計畫書之安全衛生管理計畫", "Major": 1, "CheckContent": [ { "Major": 1, "Minor": 1, "CheckItem": "安全衛生管理計畫各項安全檢查" }, { "Major": 1, "Minor": 2, "CheckItem": "工地潛在危險狀況分析及採取相對應之防範措施" } ] } ] }
//======================================================================================================== //讀取 Json 資料 func readJson() { if let path = Bundle.main.path(forResource: "JsonFile/CheckList", ofType: "json") { do { let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped) if let myJSON = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any] { //檢查項目 if let data = myJSON["CheckList"] as? [[String: Any]] { for jsonDict in data { var CheckItem = jsonDict["CheckItem"] as? String print("CheckItem:" + CheckItem!) var Major = jsonDict["Major"] as? Int print("Major:" + String(Major!)) //檢查內容 if let CheckContent = jsonDict["CheckContent"] as? [[String: Any]] { for CheckContentDict in CheckContent { var CheckItem = CheckContentDict["CheckItem"] as? String print("CheckItem:" + CheckItem!) var Major = CheckContentDict["Major"] as? Int print("Major:" + String(Major!)) var Minor = CheckContentDict["Minor"] as? Int print("Minor:" + String(Minor!)) } } } } } } catch let error { print("parse error: \(error.localizedDescription)") } } else { print("Invalid filename/path.") } }
沒有留言:
張貼留言