DBA

Kakao API : address 응답 JSON을 Swift Structure로 만들기 본문

[3] Development/Swift

Kakao API : address 응답 JSON을 Swift Structure로 만들기

코볼 2023. 3. 23. 13:46
728x90
반응형
SMALL

요청 주소 : 서울 강남구 역삼동

 

[JSON Result]

{
    "documents": [
        {
            "address": {
                "address_name": "서울 강남구 역삼동",
                "b_code": "1168010100",
                "h_code": "",
                "main_address_no": "",
                "mountain_yn": "N",
                "region_1depth_name": "서울",
                "region_2depth_name": "강남구",
                "region_3depth_h_name": "",
                "region_3depth_name": "역삼동",
                "sub_address_no": "",
                "x": "127.03306536185",
                "y": "37.4953666908089"
            },
            "address_name": "서울 강남구 역삼동",
            "address_type": "REGION",
            "road_address": null,
            "x": "127.03306536185",
            "y": "37.4953666908089"
        },
        {
            "address": {
                "address_name": "서울 강남구 역삼1동",
                "b_code": "",
                "h_code": "1168064000",
                "main_address_no": "",
                "mountain_yn": "N",
                "region_1depth_name": "서울",
                "region_2depth_name": "강남구",
                "region_3depth_h_name": "역삼1동",
                "region_3depth_name": "",
                "sub_address_no": "",
                "x": "127.033201083326",
                "y": "37.4954171091244"
            },
            "address_name": "서울 강남구 역삼1동",
            "address_type": "REGION",
            "road_address": null,
            "x": "127.033201083326",
            "y": "37.4954171091244"
        },
        {
            "address": {
                "address_name": "서울 강남구 역삼2동",
                "b_code": "",
                "h_code": "1168065000",
                "main_address_no": "",
                "mountain_yn": "N",
                "region_1depth_name": "서울",
                "region_2depth_name": "강남구",
                "region_3depth_h_name": "역삼2동",
                "region_3depth_name": "",
                "sub_address_no": "",
                "x": "127.046839198832",
                "y": "37.4959639052057"
            },
            "address_name": "서울 강남구 역삼2동",
            "address_type": "REGION",
            "road_address": null,
            "x": "127.046839198832",
            "y": "37.4959639052057"
        }
    ],
    "meta": {
        "is_end": true,
        "pageable_count": 3,
        "total_count": 3
    }
}

 

 

 

[Swift Structure]

        struct SearchResult: Codable {

            let documents: [Document]

            let meta: Meta

        }

 

        struct Document: Codable {

            let address: Address

            let addressName: String

            let addressType: String

            let roadAddress: String?

            let x: String

            let y: String

            

            enum CodingKeys: String, CodingKey {

                case address

                case addressName = "address_name"

                case addressType = "address_type"

                case roadAddress = "road_address_name"

                case x

                case y

            }

        }

 

        struct Address: Codable {

            let addressName: String

            let bCode: String

            let hCode: String

            let mainAddressNo: String

            let mountainYN: String

            let region1DepthName: String

            let region2DepthName: String

            let region3DepthHName: String

            let region3DepthName: String

            let subAddressNo: String

            let x: String

            let y: String

            

            enum CodingKeys: String, CodingKey {

                case addressName = "address_name"

                case bCode = "b_code"

                case hCode = "h_code"

                case mainAddressNo = "main_address_no"

                case mountainYN = "mountain_yn"

                case region1DepthName = "region_1depth_name"

                case region2DepthName = "region_2depth_name"

                case region3DepthHName = "region_3depth_h_name"

                case region3DepthName = "region_3depth_name"

                case subAddressNo = "sub_address_no"

                case x

                case y

            }

        }

 

        struct Meta: Codable {

            let isEnd: Bool

            let pageableCount: Int

            let totalCount: Int

            

            enum CodingKeys: String, CodingKey {

                case isEnd = "is_end"

                case pageableCount = "pageable_count"

                case totalCount = "total_count"

            }

        }

 

 

728x90
반응형
LIST
Comments