将 JSON 字段值映射到数据 class 值

Map JSON field values to data class values

我得到了一个 JSON 字符串,我想稍微清理一下并减少 return 数据。但是,我无法弄清楚如何从 JSON 中获取值并将其正确附加到我创建的数据 class 中。

这里是传入的JSON:

{
  "continent": {
    "code": "EU",
    "names": {
      "de": "Europa",
      "ru": "Европа",
      "pt-BR": "Europa",
      "ja": "ヨーロッパ",
      "en": "Europe",
      "fr": "Europe",
      "zh-CN": "欧洲",
      "es": "Europa"
    },
    "geoname_id": 6255148
  },
  "country": {
    "is_in_european_union": true,
    "names": {
      "de": "Frankreich",
      "ru": "Франция",
      "pt-BR": "França",
      "ja": "フランス共和国",
      "en": "France",
      "fr": "France",
      "zh-CN": "法国",
      "es": "Francia"
    },
    "iso_code": "FR",
    "geoname_id": 3017382
  },
  "city": {
    "geoname_id": 2977058,
    "names": {
      "en": "Saint-Saulve",
      "ru": "Сен-Сольв",
      "zh-CN": "圣索尔沃"
    }
  },
  "location": {
    "accuracy_radius": 10,
    "time_zone": "Europe/Paris",
    "latitude": 50.3714,
    "longitude": 3.5561
  },
  "postal": {
    "code": "59880"
  },
  "registered_country": {
    "is_in_european_union": true,
    "names": {
      "de": "Frankreich",
      "ru": "Франция",
      "pt-BR": "França",
      "ja": "フランス共和国",
      "en": "France",
      "fr": "France",
      "zh-CN": "法国",
      "es": "Francia"
    },
    "iso_code": "FR",
    "geoname_id": 3017382
  },
  "subdivisions": [
    {
      "names": {
        "fr": "Hauts-de-France",
        "en": "Hauts-de-France"
      },
      "iso_code": "HDF",
      "geoname_id": 11071624
    },
    {
      "names": {
        "de": "Nord",
        "en": "North",
        "fr": "Nord",
        "es": "Norte"
      },
      "iso_code": "59",
      "geoname_id": 2990129
    }
  ]
}

数据类:

data class locationData(val accuracyRadius: String = "", val timeZone: String = "", val latitude: String = "",
                        val longitude: String = "", val postalCode: String = "", val continent: Continent,
                        val country: Country, val city: City, val subdivision: Subdivision)
data class Continent(val code: String = "", val name: String = "", val geonameId: String = "")
data class Country(val isEU: Boolean = false, val name: String = "", val isoCode: String = "", val geonameId: String = "")
data class City(val name: String = "", val geonameId: String = "")
data class Subdivision(val name: String = "", val isoCode: String = "", val geonameId: String = "")

我在遍历 json 并过滤掉我不想要的片段时的不幸尝试:

private fun cleanData(data: JsonNode): String {
    data.fieldNames().forEachRemaining { field -> 
        println(field);
    }

    val result = data.filter { item ->
        if (item.has("names")) {
            return item.get("names").has("en"); item["name"] = item.get("names").get("en");
        }
        return item.toString();
    }

    return "";

    /*val continent = {
        "code" = data.get("continent").get("code"),

    }*/
}

这就是我想要的 JSON 输出应该是:

{   
    "accuracy_radius": 10,
    "time_zone": "Europe/Paris",
    "latitude": 50.3714,
    "longitude": 3.5561,
    "postalCode": "59880",
    "continent": {
        "code": "EU",
        "name": "Europe",
        "geonameId": 6255148
    },
    "country": {
        "isEU": true,
        "name": "France",
        "isoCode": "FR",
        "geonameId": 3017382
    },
    "city": {
        "geonameId": 2977058,
        "name": "Saint-Saulve",
    },
    "subdivisions": [
        {
          "name": "Hauts-de-France"
          "isoCode": "HDF",
          "geonameId": 11071624
        },
        {
          "name": "North",
          "isoCode": "59",
          "geonameId": 2990129
        }
    ]
}

我建议使用 Moshi (https://github.com/square/moshi/) 和自定义适配器(参见 github 页面上的示例)来 filter/transform 您的数据,当您将它们读入您的数据时类。如果您已正确填充数据对象,您应该能够毫无问题地写入所需的 JSON。