Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

317
Views
Clase de datos Kotlin de Json usando GSON

Tengo una clase Java POJO como esta:

 class Topic { @SerializedName("id") long id; @SerializedName("name") String name; }

y tengo una clase de datos de Kotlin como esta

 data class Topic(val id: Long, val name: String)

¿Cómo proporcionar la json key a cualquier variable de la clase de kotlin data class como la anotación @SerializedName en variables java?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Clase de datos:

 data class Topic( @SerializedName("id") val id: Long, @SerializedName("name") val name: String, @SerializedName("image") val image: String, @SerializedName("description") val description: String )

a JSON:

 val gson = Gson() val json = gson.toJson(topic)

de JSON:

 val json = getJson() val topic = gson.fromJson(json, Topic::class.java)
over 4 years ago · Santiago Trujillo Report

0

Basado en la respuesta de Anton Golovin

Detalles

  • Versión Gson: 2.8.5
  • Estudio Android 3.1.4
  • Versión de Kotlin: 1.2.60

Solución

Cree cualquier dato de clase y herede la interfaz JSONConvertable

 interface JSONConvertable { fun toJSON(): String = Gson().toJson(this) } inline fun <reified T: JSONConvertable> String.toObject(): T = Gson().fromJson(this, T::class.java)

Uso

clase de datos

 data class User( @SerializedName("id") val id: Int, @SerializedName("email") val email: String, @SerializedName("authentication_token") val authenticationToken: String) : JSONConvertable

Desde JSON

 val json = "..." val object = json.toObject<User>()

A JSON

 val json = object.toJSON()
over 4 years ago · Santiago Trujillo Report

0

Puedes usar similar en la clase Kotlin

 class InventoryMoveRequest { @SerializedName("userEntryStartDate") @Expose var userEntryStartDate: String? = null @SerializedName("userEntryEndDate") @Expose var userEntryEndDate: String? = null @SerializedName("location") @Expose var location: Location? = null @SerializedName("containers") @Expose var containers: Containers? = null }

Y también para la clase anidada puede usar lo mismo como si hubiera un objeto anidado. Simplemente proporcione el nombre de serialización para la clase.

 @Entity(tableName = "location") class Location { @SerializedName("rows") var rows: List<Row>? = null @SerializedName("totalRows") var totalRows: Long? = null }

por lo tanto, si obtiene una respuesta del servidor, cada clave se asignará a JOSN.

Alos, convierte List a JSON:

 val gson = Gson() val json = gson.toJson(topic)

Android convertir de JSON a objeto:

 val json = getJson() val topic = gson.fromJson(json, Topic::class.java)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!