将Json转换为数据类Kotlin

v9tzhpje  于 2022-12-13  发布在  Kotlin
关注(0)|答案(1)|浏览(180)

我是android开发领域的新手。我正在使用retrofit和协程。尝试将下一个json对象转换为数据类。

{
        "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": "🇩🇪",
        "maps": {
            "googleMaps": "https://goo.gl/maps/mD9FBMq1nvXUBrkv6",
            "openStreetMaps": "https://www.openstreetmap.org/relation/51477"
        },
        "population": 83240525,
        "timezones": [
            "UTC+01:00"
        ],
        "continents": [
            "Europe"
        ],
        "flags": {
            "png": "https://flagcdn.com/w320/de.png",
            "svg": "https://flagcdn.com/de.svg"
        },
        "capitalInfo": {
            "latlng": [
                52.52,
                13.4
            ]
        }
    }

`
在德国的情况下,lengagues json只有一个项目,currencies json有一个EUR项目。你也可以看到名称json发生了变化。我应该如何使用Kotlin转换为数据类?
我试着寻找解决方案,但还没有真正找到任何适合我的情况。

2w2cym1i

2w2cym1i1#

您需要创建如下所示的数据类:

data class Name(
    val common: String,
    val official: String,
    val nativeName: Map<String, NativeName>,
)

data class NativeName(
    val official: String,
    val common: String,
)

基本数据类中的货币也是这样的:

val currencies: Map<String, Currency>

Currency类应该如下所示:

data class Currency(
    val name: String,
    val symbol: String,
)

相关问题