使用 Jackson 将 Json 字符串解析为 Kotlin 对象抛出 InvalidDefinitionException

刁曼岛

我从 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 模块,该模块例如添加了对数据类的支持。

添加对 Kotlin 类和数据类的序列化/反序列化支持的模块。以前,Kotlin 对象上必须存在默认构造函数,Jackson 才能反序列化为对象。有了这个模块,可以自动使用单个构造函数类,也支持带有二级构造函数或静态工厂的类。

有了它,您可以创建一个ObjectMapper已经使用com.fasterxml.jackson.module.kotlin.ExtensionsKt#jacksonObjectMapper. (注意您还可以如何使用它提供的扩展之一来避免声明有效负载类)

var mapper = jacksonObjectMapper()
val movie: Movie = mapper.readValue(payload)

作为替代方案,您仍然可以使用通常的 API 创建对象映射器并注册模块:

var mapper = ObjectMapper()
mapper.registerModule(KotlinModule())

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用Jackson将JSON解析为对象

如何使用Jackson将JSON字符串解析为数组

使用Jackson将JSON字符串解析为原始Java类型?

如何使用Jackson将JSON对象键解析为整数?

使用Jackson将列表或对象的JSON内容解析为List <A>

将 Javascript 对象解析为类似 JSON 的字符串,但使用单引号

将JSON查询字符串解析为JSON对象

无法将字符串Json解析为Json对象

Kotlin vert.x使用gson将JSON字符串解析为Data类始终返回null

将 JSON 从字符串解析为对象

Nodejs - 将字符串解析为 JSON 对象

将JSON对象解析为Java中的字符串

将字符串转换或解析为JSON对象

如何使用 Jackson 将 JSON 字符串序列化为 JAVA 对象

使用Jackson将JSON字符串或对象反序列化为String字段

使用PowerShell无法将字符串解析为对象

使用Jackson的ObjectMapper解析具有动态类型(字符串或对象)的值的JSON

使用GSON将字符串解析为JsonObject会产生IllegalStateException:这不是JSON对象

使用Aeson将JSON解析为映射字符串字符串

如何使用Jackson的ObjectMapper从JSON字符串创建对象?

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

如何将数据类解析为Kotlin的JSON字符串?

将JSON字符串解析为Kotlin上的JsonObject / Map / MutableMap

使用 jackson Parser 解析 Json 字符串

如何在SpringBoot中使用Jackson将JSON对象数组解析为DTO

com.fasterxml.jackson.databind.exc.MismatchedInputException,同时使用fastxml将json回复解析为对象

Jackson将JSON对象解析为对象数组

使用JSON.NET将动态JSON字符串解析为C#中的字符串

将字符串解析为JSON