Kotlin:GSON fromJson 不计算

Kotlin: GSON fromJson doesn't compute

我成功接收了一个 JSON 对象作为请求并将其传递给我的解析器。代码运行直到我调用 fromJson 然后卡住。我做错了什么?

这里是对应的class:

class User(@SerializedName("mac") private val phoneMac: String) : Comparable<User> {

    @SerializedName("values")
    private val measurements: MutableSet<Measurement> = mutableSetOf()

    fun getPhoneMac(): String = phoneMac
    fun getMeasurements(): Set<Measurement> = measurements

    //etc.
}

指的是这个class:

class Measurement (@SerializedName("mac") val deviceMac: String, val timestamp: String, val value: Double, val valueType: ValueType) : Comparable<Measurement>{

    fun getDeviceMac(): String = deviceMac
    fun getTimestamp(): String = timestamp
    fun getValue(): Double = value
    fun getValueType(): ValueType = valueType

        //etc.
}

下面是我尝试解析它的方式:

fun fromJson(json: String): User {
    val builder = GsonBuilder()
    builder.setPrettyPrinting()

    return builder.create().fromJson(json, User::class.java)
}

展开 fromJson 函数以确保它被卡住的地方:create() 仍然有效,fromJson() 无效

此外,我知道 JSON 文件是正确的,不包含缺失值或空值。

验证:

{
"mac": "00-80-41-ae-fd-b1",
"values": 
[
    {
    "mac": "ab-cd-ef-98-76-13",
    "timestamp": "2012-04-23T18:25:43",
    "value": 68,
    "valuetype": "HR"
    },
    {
    "mac": "ab-cd-ef-98-76-13",
    "timestamp": "2012-04-23T18:35:43",
    "value": 65,
    "valuetype": "HR"
    }
]
}

编辑:澄清我的代码卡住的意思 出于调试目的,我将 fromJson 函数更改为如下所示:

fun fromJson(json: String): User {
    val builder = GsonBuilder()
    builder.setPrettyPrinting()

    println("json received")
    val gson = builder.create()
    println("GSON created")
    val user = gson.fromJson(json, User::class.java)
    println("user created")

    return user
}

我的控制台显示

json received GSON created

意思是 "user created" 没有被打印出来,因此 gson.fromJson-call 永远不会 returns

不确定卡住是什么意思,但这似乎有效:

import com.google.gson.*
import com.google.gson.annotations.*

data class User(@SerializedName("mac") val phoneMac: String, @SerializedName("values") val measurements: MutableSet<Measurement>)

enum class ValueType{
    HR
}

data class Measurement (@SerializedName("mac") val deviceMac: String, val timestamp: String, val value: Double, val valuetype: ValueType)

fun fromJson(json: String): User {
    val builder = GsonBuilder()
    builder.setPrettyPrinting()

    return builder.create().fromJson(json, User::class.java)
}

fun main() {
    println(fromJson("""
        {
        "mac": "00-80-41-ae-fd-b1",
        "values": 
        [
            {
            "mac": "ab-cd-ef-98-76-13",
            "timestamp": "2012-04-23T18:25:43",
            "value": 68,
            "valuetype": "HR"
            },
            {
            "mac": "ab-cd-ef-98-76-13",
            "timestamp": "2012-04-23T18:35:43",
            "value": 65,
            "valuetype": "HR"
            }
        ]
        }
    """.trimIndent()))
}

似乎解决方法是删除 Measurement-class 中的 getter 函数或将 Measurement-class 中的字段设置为私有。

我copy/pasted barsju 的代码,它起作用了。所以一步一步地尝试交换我的代码片段。在我的 Measurments-class 交换后,我得到以下异常:

java.lang.ClassFormatError: Duplicate method name "getDeviceMac" with signature "()Ljava.lang.String;" in class file jsonStuff/Measurement

做了一些实验和研究才发现,如果 Measurement 的字段不是私有的,但仍然有 getter 显式声明的函数,它们会被识别为重复函数,基本上会杀死应用程序

我建议,如果可以的话,使用 data classes,并删除 getter 和 Comparable。 这也是一些用户在这里给你的示例答案的不同之处之一。