@GET("/3/discover/movie?api_key=${apiKey}&language=en-US&sort_by=popularity.desc&include_adult=false&include_video=false&page=1")
fun getMovies(): Single<List<Movie>>
}
private val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build()
.create(MoviesApi::class.java)
fun getMovies(): Single<List<Movie>> {
return api.getMovies()
}
{
"page": 1,
"total_results": 10000,
"total_pages": 500,
"results": [
{
"popularity": 259.91,
"vote_count": 366,
"video": false,
"poster_path": "/aQvJ5WPzZgYVDrxLX4R6cLJCEaQ.jpg",
"id": 454626,
"adult": false,
"backdrop_path": "/qonBhlm0UjuKX2sH7e73pnG0454.jpg",
"original_language": "en",
"original_title": "Sonic the Hedgehog",
"genre_ids": [
28,
35,
878,
10751
],
"title": "Sonic the Hedgehog",
"vote_average": 7.1,
"overview": "Based on the global blockbuster videogame franchise from Sega, Sonic the Hedgehog tells the story of the world’s speediest hedgehog as he embraces his new home on Earth. In this live-action adventure comedy, Sonic and his new best friend team up to defend the planet from the evil genius Dr. Robotnik and his plans for world domination.",
"release_date": "2020-02-12"
},
{
"popularity": 253.357,
"vote_count": 4333,
"video": false,
"poster_path": "/7IiTTgloJzvGI1TAYymCfbfl3vT.jpg",
"id": 496243,
"adult": false,
"backdrop_path": "/TU9NIjwzjoKPwQHoHshkFcQUCG.jpg",
"original_language": "ko",
"original_title": "기생충",
"genre_ids": [
35,
18,
53
],
"title": "Parasite",
"vote_average": 8.6,
"overview": "All unemployed, Ki-taek's family takes peculiar interest in the wealthy and glamorous Parks for their livelihood until they get entangled in an unexpected incident.",
"release_date": "2019-05-30"
},
{
"popularity": 213.161,
"vote_count": 2415,
"video": false,
"poster_path": "/xBHvZcjRiWyobQ9kxBhO6B2dtRI.jpg",
"id": 419704,
"adult": false,
"backdrop_path": "/5BwqwxMEjeFtdknRV792Svo0K1v.jpg",
"original_language": "en",
"original_title": "Ad Astra",
"genre_ids": [
12,
18,
9648,
878,
53
],
"title": "Ad Astra",
"vote_average": 6,
"overview": "The near future, a time when both hope and hardships drive humanity to look to the stars and beyond. While a mysterious phenomenon menaces to destroy life on planet Earth, astronaut Roy McBride undertakes a mission across the immensity of space and its many perils to uncover the truth about a lost expedition that decades before boldly faced emptiness and silence in search of the unknown.",
"release_date": "2019-09-17"
},
{
"popularity": 171.658,
"vote_count": 776,
"video": false,
"poster_path": "/h4VB6m0RwcicVEZvzftYZyKXs6K.jpg",
"id": 495764,
"adult": false,
"backdrop_path": "/uozb2VeD87YmhoUP1RrGWfzuCrr.jpg",
"original_language": "en",
"original_title": "Birds of Prey (and the Fantabulous Emancipation of One Harley Quinn)",
"genre_ids": [
28,
35,
80
],
"title": "Birds of Prey (and the Fantabulous Emancipation of One Harley Quinn)",
"vote_average": 6.8,
"overview": "After her breakup with the Joker, Harley Quinn joins forces with singer Black Canary, assassin Huntress, and police detective Renee Montoya to help a young girl named Cassandra, who had a hit placed on her after she stole a rare diamond from crime lord Roman Sionis.",
"release_date": "2020-02-05"
},
{
"popularity": 154.297,
"vote_count": 2258,
"video": false,
"poster_path": "/pThyQovXQrw2m0s9x82twj48Jq4.jpg",
"id": 546554,
"adult": false,
"backdrop_path": "/cjTQSwcsfVdirSFSHNBXRGkxmWa.jpg",
"original_language": "en",
"original_title": "Knives Out",
"genre_ids": [
35,
80,
18,
9648,
53
]
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
data class Movie(
val page: Int?,
val total_results: Int?,
val total_pages: Int?,
val results: List<MovieData>
)
data class MovieData(
val popularity: Int?,
val vote_count: Int?,
val video: Boolean?,
val poster_path: String?,
val id: Int?,
val adult: Boolean?,
val backdrop_path: String?,
val original_language: String?,
val original_title: String?,
val genre_ids: List<Int>?,
val title: String?,
val vote_average: Int?,
val overview: String?,
val release_date: String?
)
基本上,您的响应是一个JSON对象,而您试图将其解析为一个JSON数组。您的模型类movie
是您获得的响应。因此,如果您想要完整的响应,请使用
fun getMovies(): Single<Movie>
这将给出完整的响应,其中包含results:list
的值以及返回的其他参数。
希望这能帮到你
问题内容: 我必须与API进行交互,并且响应格式(根据我的阅读)似乎结构不良。我发现一个Google 网上论坛在这里回答了一个类似的问题,但是我在实现Response类来处理Gson.fromJson时遇到了麻烦。有没有我想念的例子? 问题答案: JSON对象可以由或Javabean类表示。这是一个使用Javabean的示例。 如下使用它:
问题内容: 我正在尝试从上述JSON检索邮政编码。我正在用gson解析它。我是JSON的新手,从我在这里的所有文章中读到的内容(有些与此类似),我都知道字段名称应保持原样。所以我知道我必须做出4类,即响应,视图,结果和地址。我使它们成为静态嵌套类,但是我只得到空值作为输出。在下一个JSON中,我有多个地址。但是我只停留在这个单一的回应上。 举一个简短的例子,我尝试使用此代码检索Timestamp,
我想做的是使用Gson将嵌套的json数据解析为Java对象,并使用自定义的toString()将其打印出来。 Json内容 POJO类:示例 POJO类:JsonFormatter 我的POJO还有其他类,我试图将其解析为Gson的方式是: 但是当我试图打印出它的子值时,比如 我得到了错误: 我想实现的是将上面的json内容打印成这样: 谁能帮我解决这个问题吗?提前谢谢!
问题内容: 我正在尝试使用具有以下结构的Java中的gson解析一些JSON数据,但是通过在线查看示例,我找不到任何能完成此工作的东西。 有人可以协助吗? 问题答案: 您只需要创建一个Java类结构即可表示JSON中的数据。为了做到这一点,我建议您将JSON复制到此在线JSON Viewer中 ,您会发现JSON的结构更加清晰… 基本上,您需要这些类(伪代码): 请注意,您的类中的属性名称必须与J
假设我期望一个列表列表,其中内部列表具有不同的类型和长度,例如。G 如何使用argparse解析上述列表? 关于stackoverflow最有用的问题: 类似的问题也存在,最有用的问题在这里。但是在我的例子中,它们还不够好,因为它们忽略了列表嵌套了不同的数据类型和长度的事实。
编写我自己的JSON转换器工厂,它扩展converter.factory. 以字符串类型返回原始响应,并自己解析。但是根据我最初的研究,从改型2.0中得到原始的响应并不容易。Retrofit 2.0似乎坚持在将响应传递给我之前将其转换为某个内容,而且Retrofit没有提供自己的。(我可能错了~) Update:我们实际上可以通过将设置为HTTP API接口的pojo,并使用Retrofit提供的