English 中文(简体)
如何使包含另一个数据类别的数据类别脱轨
原标题:How to deserialize a data class that contains another data class

我有两个数据类别A和B

data class A(val b: B, val text: String)

data class B(
    @JsonProperty("b") val number: Int,
    @JsonProperty(access = JsonProperty.Access.READ_ONLY) val date: String = "",
)

My objectmapper is defined like this: val objectMapper = ObjectMapper().apply { registeredMoudle(KotlinModule())}

主要:

val json = "{"b": 5, "text": "blabla"}"
val a = objectMapper.readValue(json,A::class.java) 

我获得的是错误的输出:无法证实B(尽管至少有一个造物者存在):没有固定的构造/推算法从强势价值中脱硫(5)

这正是我试图使B自食其力的时候。 因此,我不理解为什么会把它推上A的工作。

问题回答

这是因为您的json的编排方式,b 应当是目标,而不是扼杀。

换言之,如果你有两班,这(建议的json)就赢得了笔工作:

val json = “{”b: “5”、“text”:“bla"}>

但是,

val json2 = "{"b": { "b": 5 }, "text": "blabla"}"





相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签