도래울

swift3 json 파일 읽기 본문

카테고리 없음

swift3 json 파일 읽기

도래울 2017. 7. 12. 16:24


data에서 json은 SwiftyJSon 라이브러리를 이용.



filename : test.json


if let path = Bundle.main.path(forResource: "test", ofType: "json") {

            do {

                let data = try? Data(contentsOf: URL(fileURLWithPath: path), options: NSData.ReadingOptions.mappedIfSafe)

                let jsonObj = JSON(data: data!)

                if jsonObj != JSON.null {


   print(jsonObj.arrayObject)


                } else {

                    print("could not get json from file, make sure that file contains valid json.")

                }

            }

        } else {

            print("Invalid filename/path.")

        }

Comments