当我在 JSON 上没有主要元素时,Retrofit Returns 所需的价值公园在 $ 处缺失
Retrofit Returns Required Value parks missing at $ When I do not have main element on JSON
总结:我正在尝试从国家公园 (API) 中提取数据,但它有 JSON 没有主要元素名称。而且似乎改造会自动查找变量名称以查找元素。它无法返回错误 'Required Value parks missing at $'。我没有公园,但我根本不是在寻找公园元素。我搜索了 Whosebug 以查看是否有人有类似的问题但没有找到所以我在这里发布这个问题。
详细信息:这是来自 API...
的 json
{"total":"498","data":[{"contacts":{"phoneNumbers":[{"phoneNumber":"9372257705","description": "","extension":"","type":"Voice"}],"emailAddresses":[{"description":"","emailAddress":"tom_engberg@nps.gov"}]},"states":"OH","longitude":"-84.0711364746094","activities":[{"id":"B33DC9B6-0B7D-4322-BAD7-A13A34C584A3" ,"name":"Guided Tours"}],"entranceFees":[{"cost":"1.0000","description":"The National Aviation Heritage Area is comprised of many sites. While some sites are free of charge to the public, others may have entrance fees and\/or event or participation fees. Please check on the specific National Aviation Heritage Area site prior to your visit.","title" :"Entrance Fees Vary"}],"directionsInfo":"The National Aviation Heritage Area has multiple sites located throughout eight counties in the Dayton, Ohio and western Ohio area. Please be sure to visit a specific National Aviation Heritage Area website for directions and\/or maps to each location.","entrancePasses":[{"cost":"1.0000","description":"The National Aviation Heritage Area is comprised of many sites. While some sites are free of charge to the public, others may have entrance fees and\/or event or participation fees. Please check on the specific National Aviation Heritage Area site prior to your visit." ,"title":"Fee Pass Costs Vary"}],"directionsUrl":"http:\/\/www.aviationheritagearea.org\/","url":"https:\/\/www.nps.gov\/avia\/index.htm","weatherInfo":"The National Aviation Heritage Area lies in a humid continental zone with a generally temperate climate. Winters are mildly cold with average temperatures around 39 degrees (F). Summers are hot and humid with an average temperature around 74 degrees (F). Average annual total rainfall is just above 41\" .冬季的降雪量普遍较小,平均总降雪量约为 25".","name":"National Aviation","operatingHours":[{"exceptions":[],"description":"While this website is not meant to be an exhaustive resource for all of the National Aviation Heritage Area partners and organizations, there is an official partner organization (National Aviation Heritage Alliance) which operates a separate and full-functioning website with a plethora of site information. Visit National Aviation Heritage Alliances' webpage for up-to-date information, directions and breaking news for all of the historical sites and member organizations.","standardHours":{"wednesday":"All Day","monday":"All Day","thursday":"All Day","sunday":"All Day","tuesday":"All Day","friday":"All Day","saturday":"All Day"},"name":"Various Heritage Area Sites"}],"topics":[{"id":"B912363F-771C-4098-BA3A-938DF38A9D7E","name":"Aviation"}] ,"latLong":"lat:39.9818229675293, long:-84.0711364746094","description":"Aviation is chock-full of tradition & history and nowhere will you find a richer collection of aviation than here, the birthplace of aviation. From the straightforward bicycle shops that fostered the Wright brothers' flying ambitions to the complex spacecraft that carried man to the moon, the National Aviation Heritage Area has everything you need to learn about this country’s aviation legacy.","images":[{"credit":"NPS Photo \/ Tom Engberg","altText" :"Visitor center building in background with plaza in foreground","title":"Dayton's National Park","caption":"The Wright-Dunbar Interpretive Center located just west of downtown Dayton","url":"https:\/\/www.nps.gov\/common\/uploads\/structured_data\/DCB2628F-1DD8-B71B-0BD78D1063069C70.jpg"}],"designation" :"Heritage Area","parkCode":"avia","addresses":[{"postalCode":"45402","city":"Dayton","stateCode":"OH","line1":"16 South Williams St.","type":"Physical","line3":"","line2":""},{"postalCode":"45402","city":"Dayton","stateCode":"OH","line1":"16 南威廉姆斯街。","type":"Mailing","line3":"","line2":""}],"id":"C8C207D8-49C4-4891-9915-0007205A0284","fullName":"National Aviation Heritage Area","latitude":"39.9818229675293"}],"limit":"1","start":"1"}
以下是我写的数据class:
data class NetworkParkContainer(val parks: List<NetworkPark>)
data class NetworkPark(
val data: List<Data>,
val limit: String,
val start: String,
val total: String
)
这是我为获取 JSON 数据而调用的代码:
interface ParksApiService {
companion object {
val API_KEY = "<my own api key value>"
}
@GET("parks")
suspend fun getParks(@Query("api_key") type: String, @Query("limit") limit: Int): NetworkParkContainer
}
为什么 Retrofit 2 会寻找 'parks'?我的任何数据 class 中根本没有 'parks'。它似乎在 :
中查看 'parks'
data class NetworkParkContainer(val parks: List<NetworkPark>)
我怎么告诉改造我想要所有它就像我写数据 class NetworkPark 甚至不包括 'parks'?
这是 API 路径:
https://developer.nps.gov/api/v1/parks?api_key=&limit=1
我的基础URL是https://developer.nps.gov/api/v1/
所以我的@GET 是 parks 因此,它应该是这样的
https://developer.nps.gov/api/v1/parks?api_key=&limit=1
您的数据 class 映射错误。您需要确保来自 API 的密钥与数据 class 具有相同的名称,因为 GSON 使用这些值,而不是改造。 Retrofit 只是获取数据,然后将其传递给 GSON 进行反序列化。如果 API returns 奇怪的名字并且你不希望在你的数据中出现 classes 然后使用 @SerializedName("newName: String") 注释关于数据中的参数 class.
解决方案
data class NetworkPark(
val total: String,
val data: List<Data>,
val limit: String,
val start: String
)
此数据class是多余的:
data class NetworkParkContainer(val parks: List<NetworkPark>)
界面变化:
interface ParksApiService {
companion object {
val API_KEY = "<my own api key value>"
}
@GET("parks")
suspend fun getParks(@Query("api_key") type: String, @Query("limit") limit: Int): NetworkPark
}
总结:我正在尝试从国家公园 (API) 中提取数据,但它有 JSON 没有主要元素名称。而且似乎改造会自动查找变量名称以查找元素。它无法返回错误 'Required Value parks missing at $'。我没有公园,但我根本不是在寻找公园元素。我搜索了 Whosebug 以查看是否有人有类似的问题但没有找到所以我在这里发布这个问题。
详细信息:这是来自 API...
的 json{"total":"498","data":[{"contacts":{"phoneNumbers":[{"phoneNumber":"9372257705","description": "","extension":"","type":"Voice"}],"emailAddresses":[{"description":"","emailAddress":"tom_engberg@nps.gov"}]},"states":"OH","longitude":"-84.0711364746094","activities":[{"id":"B33DC9B6-0B7D-4322-BAD7-A13A34C584A3" ,"name":"Guided Tours"}],"entranceFees":[{"cost":"1.0000","description":"The National Aviation Heritage Area is comprised of many sites. While some sites are free of charge to the public, others may have entrance fees and\/or event or participation fees. Please check on the specific National Aviation Heritage Area site prior to your visit.","title" :"Entrance Fees Vary"}],"directionsInfo":"The National Aviation Heritage Area has multiple sites located throughout eight counties in the Dayton, Ohio and western Ohio area. Please be sure to visit a specific National Aviation Heritage Area website for directions and\/or maps to each location.","entrancePasses":[{"cost":"1.0000","description":"The National Aviation Heritage Area is comprised of many sites. While some sites are free of charge to the public, others may have entrance fees and\/or event or participation fees. Please check on the specific National Aviation Heritage Area site prior to your visit." ,"title":"Fee Pass Costs Vary"}],"directionsUrl":"http:\/\/www.aviationheritagearea.org\/","url":"https:\/\/www.nps.gov\/avia\/index.htm","weatherInfo":"The National Aviation Heritage Area lies in a humid continental zone with a generally temperate climate. Winters are mildly cold with average temperatures around 39 degrees (F). Summers are hot and humid with an average temperature around 74 degrees (F). Average annual total rainfall is just above 41\" .冬季的降雪量普遍较小,平均总降雪量约为 25".","name":"National Aviation","operatingHours":[{"exceptions":[],"description":"While this website is not meant to be an exhaustive resource for all of the National Aviation Heritage Area partners and organizations, there is an official partner organization (National Aviation Heritage Alliance) which operates a separate and full-functioning website with a plethora of site information. Visit National Aviation Heritage Alliances' webpage for up-to-date information, directions and breaking news for all of the historical sites and member organizations.","standardHours":{"wednesday":"All Day","monday":"All Day","thursday":"All Day","sunday":"All Day","tuesday":"All Day","friday":"All Day","saturday":"All Day"},"name":"Various Heritage Area Sites"}],"topics":[{"id":"B912363F-771C-4098-BA3A-938DF38A9D7E","name":"Aviation"}] ,"latLong":"lat:39.9818229675293, long:-84.0711364746094","description":"Aviation is chock-full of tradition & history and nowhere will you find a richer collection of aviation than here, the birthplace of aviation. From the straightforward bicycle shops that fostered the Wright brothers' flying ambitions to the complex spacecraft that carried man to the moon, the National Aviation Heritage Area has everything you need to learn about this country’s aviation legacy.","images":[{"credit":"NPS Photo \/ Tom Engberg","altText" :"Visitor center building in background with plaza in foreground","title":"Dayton's National Park","caption":"The Wright-Dunbar Interpretive Center located just west of downtown Dayton","url":"https:\/\/www.nps.gov\/common\/uploads\/structured_data\/DCB2628F-1DD8-B71B-0BD78D1063069C70.jpg"}],"designation" :"Heritage Area","parkCode":"avia","addresses":[{"postalCode":"45402","city":"Dayton","stateCode":"OH","line1":"16 South Williams St.","type":"Physical","line3":"","line2":""},{"postalCode":"45402","city":"Dayton","stateCode":"OH","line1":"16 南威廉姆斯街。","type":"Mailing","line3":"","line2":""}],"id":"C8C207D8-49C4-4891-9915-0007205A0284","fullName":"National Aviation Heritage Area","latitude":"39.9818229675293"}],"limit":"1","start":"1"}
以下是我写的数据class:
data class NetworkParkContainer(val parks: List<NetworkPark>)
data class NetworkPark(
val data: List<Data>,
val limit: String,
val start: String,
val total: String
)
这是我为获取 JSON 数据而调用的代码:
interface ParksApiService {
companion object {
val API_KEY = "<my own api key value>"
}
@GET("parks")
suspend fun getParks(@Query("api_key") type: String, @Query("limit") limit: Int): NetworkParkContainer
}
为什么 Retrofit 2 会寻找 'parks'?我的任何数据 class 中根本没有 'parks'。它似乎在 :
中查看 'parks'data class NetworkParkContainer(val parks: List<NetworkPark>)
我怎么告诉改造我想要所有它就像我写数据 class NetworkPark 甚至不包括 'parks'?
这是 API 路径: https://developer.nps.gov/api/v1/parks?api_key=&limit=1
我的基础URL是https://developer.nps.gov/api/v1/
所以我的@GET 是 parks 因此,它应该是这样的 https://developer.nps.gov/api/v1/parks?api_key=&limit=1
您的数据 class 映射错误。您需要确保来自 API 的密钥与数据 class 具有相同的名称,因为 GSON 使用这些值,而不是改造。 Retrofit 只是获取数据,然后将其传递给 GSON 进行反序列化。如果 API returns 奇怪的名字并且你不希望在你的数据中出现 classes 然后使用 @SerializedName("newName: String") 注释关于数据中的参数 class.
解决方案
data class NetworkPark(
val total: String,
val data: List<Data>,
val limit: String,
val start: String
)
此数据class是多余的:
data class NetworkParkContainer(val parks: List<NetworkPark>)
界面变化:
interface ParksApiService {
companion object {
val API_KEY = "<my own api key value>"
}
@GET("parks")
suspend fun getParks(@Query("api_key") type: String, @Query("limit") limit: Int): NetworkPark
}