我是新使用改型的,所以我有这个来自服务器的JSON
{
"results_found": 572,
"results_start": 0,
"results_shown": 20,
"restaurants": [
{
"restaurant": {
"R": {
"has_menu_status": {
"delivery": -1,
"takeaway": -1
},
"res_id": 18941862
},
"apikey": "c3abca45d8387e5b1c900563f1d63193",
"id": "18941862",
"name": "Pizza Maru",
"url": "https://www.zomato.com/jakarta/pizza-maru-1-thamrin?utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1",
"location": {
"address": "Grand Indonesia Mall, East Mall, Lantai 3A, Jl. M.H. Thamrin No. 1, Thamrin, Jakarta",
"locality": "Grand Indonesia Mall, Thamrin",
"city": "Jakarta",
"city_id": 74,
"latitude": "-6.1954467635",
"longitude": "106.8216102943",
"zipcode": "",
"country_id": 94,
"locality_verbose": "Grand Indonesia Mall, Thamrin, Jakarta"
},
"switch_to_order_menu": 0,
"cuisines": "Pizza",
"timings": "10 AM to 10 PM",
"average_cost_for_two": 180000,
"price_range": 3,
"currency": "IDR",
"highlights": [
"Credit Card",
"Delivery",
"No Alcohol Available",
"Dinner",
"Debit Card",
"Lunch",
"Cash",
"Takeaway Available",
"VAT",
"Air Conditioned",
"Wifi",
"Service Charge",
"Indoor Seating",
"Table booking recommended"
],
"offers": [],
"opentable_support": 0,
"is_zomato_book_res": 1,
"mezzo_provider": "ZOMATO_BOOK",
"is_book_form_web_view": 0,
"book_form_web_view_url": "",
"thumb": "https://b.zmtcdn.com/data/pictures/chains/2/18941862/403aa36cb046e86a694e7989bb7cd545.jpg?fit=around%7C200%3A200&crop=200%3A200%3B%2A%2C%2A",
"user_rating": {
"aggregate_rating": "4.4",
"rating_text": "Sangat Baik",
"rating_color": "5BA829",
"rating_obj": {
"title": {
"text": "4.4"
},
"bg_color": {
"type": "lime",
"tint": "700"
}
},
"votes": "928"
},
"all_reviews_count": 739,
}
基地回应:
data class RestaurantListBaseResponse (
@SerializedName("results_found")
val results_found : Int = 0,
@SerializedName("results_start")
val results_start : Int = 0,
@SerializedName("results_shown")
val results_shown : Int = 0,
@SerializedName("restaurants")
val restaurants : ArrayList<Restaurant> = ArrayList()
)
餐厅类
data class Restaurant (
@SerializedName("id")
val id : Int,
@SerializedName("name")
val name : String,
@SerializedName("url")
val url : String,
@SerializedName("location")
val location : Location,
@SerializedName("currency")
val currency : String,
@SerializedName("phone_numbers")
val phone_numbers : String
)
和位置
data class Location (
@SerializedName("address")
val address : String = "",
@SerializedName("city")
val city : String = "",
@SerializedName("latitude")
val latitude : Double = 0.0,
@SerializedName("longitude")
val longitude : Double = 0.0,
@SerializedName("zipcode")
val zipcode : String = ""
)
我实际上可以将数据输入到我的android中,但是当我访问餐厅属性的值时,object属性中的所有值都变成了null
所以我在将餐厅JSON映射为Kotlin时遇到了问题。
正如您所看到的,餐厅类没有JSON的所有属性,我只是编写了一些重要的属性。这有问题吗?请帮忙。
下面是API接口
interface RestaurantAPI {
@Headers("user-key: $USER_KEY_ZOMATO")
@GET("search")
fun searchRestaurants(
@Query("radius") radius: Int,
@Query("q") searchedKeyword: String,
@Query("lat") latitude: Double,
@Query("lon") longitude: Double
): Call<RestaurantListBaseResponse>
}
object RetrofitServiceGenerator {
private val loggingInterceptor = HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
private val okHttpClient = OkHttpClient.Builder()
.callTimeout(7, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.build()
private var retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build()
fun <T> getInstance(APIType: Class<T>) : T {
return retrofit.create(APIType)
}
}
val restaurantService = RetrofitServiceGenerator.getInstance(RestaurantAPI::class.java)
val requestCall = restaurantService.searchRestaurants(1000,"pizza",-6.219225,106.834572)
requestCall.enqueue(object: Callback<RestaurantListBaseResponse> {
override fun onFailure(call: Call<RestaurantListBaseResponse>, t: Throwable) {
}
override fun onResponse(call: Call<RestaurantListBaseResponse>, response: Response<RestaurantListBaseResponse>) {
Log.d("debug","result found: ${response.body()!!.results_found}") // I can get the value
val x = response.body()!!.restaurants
Log.d("debug","list of resto: ${x.toString()}") // the properties is null
}
})
java或kotlin都可以
只需用val从数据类中删除所有初始化。或者用var代替VAL。
您已经初始化了val值,因此无法重新分配它们。
我有一个已经序列化的JSON字符串。从源序列化时,未忽略null属性值。因此,对象的序列化输出类似于: 现在,如果我在Java中使用对象映射器反序列化上面的内容,它会抛出一个异常,指向value not Available。对象映射器是否可以将“姓氏”反序列化回“null”?我可以配置对象映射器在这种情况下不会失败吗?
如何将对象转换为
用例是将对象数组转换为基于字符串或函数的哈希映射,所提供的字符串或函数用于求值并作为哈希映射中的键和作为对象本身的值。使用这种方法的一个常见情况是将对象数组转换为对象的哈希映射。 下面是JavaScript中的一个小片段,用于将对象数组转换为哈希映射,由Object的属性值索引。您可以提供一个函数来动态(运行时)评估哈希映射的键。希望这对将来的人有帮助。
我正在处理一个web服务,它一次加载ApplicationResponse对象n。我想创建一个流,它允许我使用所有这些流,而不必担心分页,因此我(不完整)使用了Java流。 我有一个生成页码的流。我想使用我以前写的函数将这些数字映射到响应流。 问题是,它似乎希望我的arrow函数返回int,而不是我所期望的流。 如何重写此表达式以使映射操作正常工作?
我想将以下字符串作为json发布到服务器。 {“FirstName”:“John”,“LastName”:“Smith”},{“FirstName”:“John”,“LastName”:“Smith”} 但是如果我使用下面的代码,我会得到json对象的数组列表。 输出: [{"FirstName":"John","LastName":"Smith" }, { "FirstName":"John",