3

私のkotlinクラスが期待どおりに機能しなかったため、少し混乱しています。

更新情報を確認するために使用されるデータ クラス:

data class UpdateInfo constructor(//kotlin class
    val description: String,
    val force: Int,
    val platform: String,
    val title: String,
    val url: String,
    @SerializedName("version")
    val versionCode: Int = 0
) : Serializable {
    val isForceUpdate = force == 1
}

また、オブジェクト形式の json をデコードするために使用されるユーティリティ:

public class JsonUtil {//java class
    private static final Gson gson;
    static {
        BooleanAdapter booleanAdapter = new BooleanAdapter();
        gson = new GsonBuilder()
            .serializeNulls()
            .disableHtmlEscaping()
            .setLenient()
            .registerTypeAdapter(Boolean.class, booleanAdapter)
            .registerTypeAdapter(boolean.class, booleanAdapter)
            .create();
    }
}

私がそれをテストするとき:

val json = "{\"force\"=\"1\"}"
val info = JsonUtil.fromJsonObject(json, UpdateInfo::class.java)
println(info)
println(info.force == 1)
println(info.isForceUpdate)

私は得る:

UpdateInfo(description=null, force=1, platform=null, title=null, url=null,versionCode=0)
true
false

何??info.isForceUpdate = false ??? 次に、または
を試しましたが、まだ機能しません。それで、どうすればいいですか..私は今直接使用していますが、なぜこれが起こったのか知りたいです.lateinitby lazy{}info.force==1

4

1 に答える 1