Home > Blockchain >  Convert Json to data class kotlin
Convert Json to data class kotlin

Time:12-12

Im a newcomer into android development world. Im using retrofit and coroutines. Trying convert the next json object into a data class. `

{
        "name": {
            "common": "Peru",
            "official": "Republic of Peru",
            "nativeName": {
                "aym": {
                    "official": "Piruw Suyu",
                    "common": "Piruw"
                },
                "que": {
                    "official": "Piruw Ripuwlika",
                    "common": "Piruw"
                },
                "spa": {
                    "official": "República del Perú",
                    "common": "Perú"
                }
            }
        },
        "tld": [
            ".pe"
        ],
        "cca2": "PE",
        "ccn3": "604",
        "cca3": "PER",
        "cioc": "PER",
        "independent": true,
        "status": "officially-assigned",
        "unMember": true,
        "currencies": {
            "PEN": {
                "name": "Peruvian sol",
                "symbol": "S/ "
            }
        },
        "idd": {
            "root": " 5",
            "suffixes": [
                "1"
            ]
        },
        "capital": [
            "Lima"
        ],
        "altSpellings": [
            "PE",
            "Republic of Peru",
            "República del Perú"
        ],
        "region": "Americas",
        "subregion": "South America",
        "languages": {
            "aym": "Aymara",
            "que": "Quechua",
            "spa": "Spanish"
        },
        "latlng": [
            -10.0,
            -76.0
        ],
        "landlocked": false,
        "borders": [
            "BOL",
            "BRA",
            "CHL",
            "COL",
            "ECU"
        ],
        "area": 1285216.0,
        "demonyms": {
            "eng": {
                "f": "Peruvian",
                "m": "Peruvian"
            },
            "fra": {
                "f": "Péruvienne",
                "m": "Péruvien"
            }
        },
        "population": 32971846,
        "timezones": [
            "UTC-05:00"
        ],
        "continents": [
            "South America"
        ],
        "flags": {
            "png": "https://flagcdn.com/w320/pe.png",
            "svg": "https://flagcdn.com/pe.svg"
        },
        "capitalInfo": {
            "latlng": [
                -12.05,
                -77.05
            ]
        }
    }

Im facing this problem: Some of the atributes change depending on the country, for Peru case lenguages json object has three items and currencies json has a PEN Item but as you can see

{
        "name": {
            "common": "Germany",
            "official": "Federal Republic of Germany",
            "nativeName": {
                "deu": {
                    "official": "Bundesrepublik Deutschland",
                    "common": "Deutschland"
                }
            }
        },
        "tld": [
            ".de"
        ],
        "cca2": "DE",
        "ccn3": "276",
        "cca3": "DEU",
        "cioc": "GER",
        "independent": true,
        "status": "officially-assigned",
        "unMember": true,
        "currencies": {
            "EUR": {
                "name": "Euro",
                "symbol": "€"
            }
        },
        "idd": {
            "root": " 4",
            "suffixes": [
                "9"
            ]
        },
        "capital": [
            "Berlin"
        ],
        "altSpellings": [
            "DE",
            "Federal Republic of Germany",
            "Bundesrepublik Deutschland"
        ],
        "region": "Europe",
        "subregion": "Western Europe",
       "languages": {
            "deu": "German"
        },
        
        "latlng": [
            51.0,
            9.0
        ],
        "landlocked": false,
        "borders": [
            "AUT",
            "BEL",
            "CZE",
            "DNK",
            "FRA",
            "LUX",
            "NLD",
            "POL",
            "CHE"
        ],
        "area": 357114.0,
        "demonyms": {
            "eng": {
                "f": "German",
                "m": "German"
            },
            "fra": {
                "f": "Allemande",
                "m": "Allemand"
            }
        },
        "flag": "           
  • Related