gpt4 book ai didi

json - 将 Json 字符串解析为 Kotlin 对象会与 Jackson 一起引发 InvalidDefinitionException

转载 作者:行者123 更新时间:2023-12-02 13:37:56 25 4
gpt4 key购买 nike

我从 API 获得了一个 Json 字符串,并希望将其解析为 Kotlin 对象。

我的杰森:

{
"Title": "The Secret Life of Walter Mitty",
"Year": "2013",
"Rated": "PG",
"Released": "25 Dec 2013",
"Runtime": "114 min",
"Genre": "Adventure, Comedy, Drama",
"Director": "Ben Stiller",
"Writer": "Steve Conrad (screenplay by), Steve Conrad (screen story by), James Thurber (based on the short story by)",
"Actors": "Ben Stiller, Kristen Wiig, Jon Daly, Kathryn Hahn",
"Language": "English, Spanish, Icelandic",
"Country": "USA, UK",
"Awards": "5 wins & 18 nominations.",
"Poster": "https://m.media-amazon.com/images/M/MV5BODYwNDYxNDk1Nl5BMl5BanBnXkFtZTgwOTAwMTk2MDE@._V1_SX300.jpg",
"Ratings": [
{
"Source": "Internet Movie Database",
"Value": "7.3/10"
},
{
"Source": "Rotten Tomatoes",
"Value": "51%"
},
{
"Source": "Metacritic",
"Value": "54/100"
}
],
"Metascore": "54",
"imdbRating": "7.3",
"imdbVotes": "265,701",
"imdbID": "tt0359950",
"Type": "movie",
"DVD": "15 Apr 2014",
"BoxOffice": "${'$'}33,223,430",
"Production": "20th Century Fox",
"Website": "http://WalterMitty.com",
"Response": "True"
}

我的 Kotlin 电影对象:
@JsonIgnoreProperties(ignoreUnknown = true)
data class Movie(

var id: Long?,

@JsonProperty("Title")
var title: String,

@JsonProperty("Released")
var release: String,

@JsonProperty("Runtime")
var runtime: String,

@JsonProperty("Genre")
var genre: String,

@JsonProperty("Director")
var director: String,

@JsonProperty("Actors")
var actor: String,

@JsonProperty("Plot")
var description: String,

@JsonProperty("Poster")
var posterLink: String?,

@JsonProperty("Metascore")
var metaScoreRating: String
)

我将 Json 解析为电影对象的部分:
...
var mapper = ObjectMapper()
mapper.readValue(jsonFromAPI, Movie::class.java)
...

但是当我运行这个片段时,我得到了这个异常:
InvalidDefinitionException: Invalid type definition for type `model.Movie`: Argument #0 of constructor [constructor for model.Movie, annotations: [null]] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator

但我不知道为什么.. Kotlin 数据类构建每个构造函数,所以我不必声明一个特定的构造函数.. 我猜 Id 有一些东西。

最佳答案

我认为您需要确保注册 jackson kotlin module例如添加对数据类的支持。

Module that adds support for serialization/deserialization of Kotlin classes and data classes. Previously a default constructor must have existed on the Kotlin object for Jackson to deserialize into the object. With this module, single constructor classes can be used automatically, and those with secondary constructors or static factories are also supported.



有了它,您可以创建一个 ObjectMapper已经使用 com.fasterxml.jackson.module.kotlin.ExtensionsKt#jacksonObjectMapper 注册了该模块. (请注意如何使用它提供的扩展之一来避免声明有效负载类)
var mapper = jacksonObjectMapper()
val movie: Movie = mapper.readValue(payload)

作为替代方案,您仍然可以创建对象映射器并使用常用 API 注册模块:
var mapper = ObjectMapper()
mapper.registerModule(KotlinModule())

关于json - 将 Json 字符串解析为 Kotlin 对象会与 Jackson 一起引发 InvalidDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51915995/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com