Tag: gson

燃料,Kotlin,Gson,预计BEGIN_ARRAY,但在BEGIN_OBJECT第1行

我试图解析一个JSON字符串,如下所示: { “count”: 1, “items”: [ { “organization_id”: 6972979, “organization_name”: “Lorem ipsum dolor sit amet, consectetur adipisicing elit”, } ] } 和Kotlin课堂: class LoremModel { var count: Int? = null var items: List? = null class Lorem { var organization_id: Int? = null var organization_name: String? = null constructor(organization_id: Int?, organization_name: String?) { this.organization_id = […]

Kotlin Gson反序列化

我正在接收具有地图包装器表的JSON数据模型。 我试图使用generics来传递超越包装的types,但它不能很好地在运行时翻译。 这里是我的JSON文件的一个例子: { “Table”: [ { “paymentmethod_id”: 1, “paymentmethod_description”: “Cash”, “paymentmethod_code”: “Cash”, “paymentmethod_is_ach_onfile”: false, “paymentmethod_is_element”: false, “paymentmethod_is_reward”: false, “paymentmethod_is_openedgeswipe”: false, “paymentmethod_update_user_id”: 1, “paymentmethod_insert_user_id”: 1, “paymentmethod_insertdate”: “2014-10-07 14:53:16”, “paymentmethod_deleted”: false, “paymentmethod_is_mobile_visible”: true } ] } 我使用的包装类叫做Table。 data class Table( @SerializedName(“Table”) val models : Array ) 实际的模型类是PaymentMethod。 data class PaymentMethod( @SerializedName(“paymentmethod_id”) val idNumber : Int = […]

如何在Kotlin中使用Gson注册一个InstanceCreator?

我可以使用代码1将MutableList保存到json字符串使用Gson正确,但是当我尝试从代码2从json字符串恢复MutableList对象时,我得到错误。我有搜索一些资源,似乎我需要注册一个InstanceCreator。 我怎样才能用Kotlin写一个InstanceCreator代码? 谢谢! 错误 Caused by: java.lang.RuntimeException: Unable to invoke no-args constructor for interface model.DeviceDef. Registering an InstanceCreator with Gson for this type may fix this problem. 代码1 private var listofMDetail: MutableList?=null mJson = Gson().toJson(listofMDetail) //Save 代码2 var mJson: String by PreferenceTool(this, getString(R.string.SavedJsonName) , “”) var aMListDetail= Gson().fromJson<MutableList>(mJson) inline fun Gson.fromJson(json: String) = this.fromJson(json, object: […]

Kotlin使用Gson的JSONParser解析对象数组

我有这些类写在kotlin,位置,其余的是在Application.kt @RealmClass open class Location( @PrimaryKey @SerializedName(“id”) var id: Int = 0, @SerializedName(“city_name”) var city_name: String? = null, @SerializedName(“elevation”) var elevation: Int = 0, @SerializedName(“state_code”) var state_code: String? = null, @SerializedName(“state_name”) var state_name: String? = null, @SerializedName(“country_code”) var country_code: String? = null, @SerializedName(“country_name”) var country_name: String? = null ):RealmObject() 其余的: private fun loadStuff() { val […]

Kotlin使用Gson将json字符串转换为ojbect列表

我有一个问题,Kotlin编写从json字符串转换为对象列表的代码。 在Java中通常是这样的: Gson gson = new Gson(); Type type = new TypeToken<List>() {}.getType(); List measurements = gson.fromJson(json, type); return measurements; 然而在Kotlin,当我尝试像这样: val gson = Gson() val type: Type = TypeToken<List>{}.type val measurements : List = gson.fromJson(text, type) return measurements IDE Android Studio强调为TypeToken所说的错误: 无法访问”:它是public / package / in’TypeToken’ 也强调说错误: types不匹配。 必需:键入! 发现:()→单位 那么有没有一种解决方案可以让Kotlin工作?

Android Studio(Kotlin)GsonBuilder.registerTypeAdapter()给出2个错误

我正在使用Kotlin插件,并尝试使用GsonBuilder创建一个gsonvariables。 这用于在Java中没有问题,但现在在尝试使用registerTypeAdapter()时,我得到了两个错误,如下所示: val gson = GsonBuilder().registerTypeAdapter(DateTime.class, DateTimeTypeConverter()).create() 对于第一个参数(types),我得到“名称预期”错误。 对于第二个参数(typeAdapter),我得到“期待expression式”的错误

与gson的Kotlin parcelize问题

我正在使用@parcelize for gson 这是我的class @Parcelize data class CommunityModel(@SerializedName(“post”) val post: PostModel, @SerializedName(“is_liked”) val isLiked: Boolean, @SerializedName(“post_like”) val postLike: QuestionModel, @SerializedName(“polling_options”) val pollingOptions: List, @SerializedName(“post_polled”) val postPolled: Boolean) : Parcelable 我得到错误Unable to invoke no-args constructor for class. Register an InstanceCreator with Gson for this type may fix this problem. Unable to invoke no-args constructor for class. […]

如何在kotlin中获得多维原始数组类?

在Java中,我一直在使用gson来解析一个像这样[[1.2, 4.1], [3.4, 4.4]] 1.2,4.1 [[1.2, 4.1], [3.4, 4.4]] 3.4,4.4 [[1.2, 4.1], [3.4, 4.4]]的JSON到一个java原始的多数组double[][] 代码看起来像这样(工作正常): String json = “[[1.2, 4.1], [3.4, 4.4]]” double[][] variable = new Gson().fromJson(json, double[][].class); 有没有办法在kotlin中获得double[][].class ? 是double[][] variable; 可以替代kotlin? 编辑: 我的目标是在kotlin中用gson实现同样的行为。 我有一千个双数组解析。 我想在kotlin中做这样的事情: val json = “[[1.1, 1.2], [2.1, 2.2, 2.3], [3.1, 3.2]” val variable:Double[][] = Gson().fromJson(json, Double[][]::class.java)

为Kotlin创建POJO类

我想为Kotlin创建POJO类,因为我们知道www.jsonschema2pojo.org将JSON转换为POJO,所以我们可以使用它与gson。 任何人都知道如何为Kotlin 快速创建Gson POJO ? 编辑: 我知道它使用Data类,但有没有最简单的方法来创建它?

使用GSON的Json的Kotlin数据类

我有这样的Java POJO类: class Topic { @SerializedName(“id”) long id; @SerializedName(“name”) String name; } 我有一个Kotlin数据类像这样 data class Topic(val id: Long, val name: String) 如何提供json selector的任何variables的kotlin data class像@SerializedName注释在Javavariables?