我需要在我的示例 kotlin 應用程式中顯示這些資料。
我已經使用 moshi 創建了所有合適的模型。可以在這里找到。
但值顯示錯誤。即使是非空值也顯示為空。
示例 json,0 索引中的值:
"main": {
"temp": 25.78,
"feels_like": 25.95,
"temp_min": 23.35,
"temp_max": 25.78,
"pressure": 1014,
"sea_level": 1014,
"grnd_level": 1012,
"humidity": 59,
"temp_kf": 2.43
},
負責模型:
@Parcelize
@JsonClass(generateAdapter = true)
data class Main(
@Json(name = "temp")
val temp: Double?,
@Json(name = "feels_like")
val feelsLike: Double?,
@Json(name = "temp_min")
val tempMin: Double?,
@Json(name = "temp_max")
val tempMax: Double?,
@Json(name = "pressure")
val pressure: Double?,
@Json(name = "sea_level")
val seaLevel: Double?,
@Json(name = "grnd_level")
val grndLevel: Double?,
@Json(name = "humidity")
val humidity: Double?,
@Json(name = "temp_kf")
val tempKf: Double?,
) : Parcelable {
fun getTempString(): String {
return temp.toString().substringBefore(".") "°"
}
fun getHumidityString(): String {
return humidity.toString() "°"
}
fun getTempMinString(): String {
return tempMin.toString().substringBefore(".") "°"
}
fun getTempMaxString(): String {
return tempMax.toString().substringBefore(".") "°"
}
}
但是當我在 0 索引中列印 main 時顯示:
Main(temp=298.26, feelsLike=null, tempMin=null, tempMax=null, pressure=1014.0, seaLevel=null, grndLevel=null, humidity=62.0, tempKf=null)
結果,這些視圖也不起作用。
完整的源代碼可以在這里找到。
uj5u.com熱心網友回復:
請參閱此處的討論,可能是以下注釋 @field:Json(name = "temp_min")而不是@Json(name = "temp_min"),并且對于其他成員也是如此,可以修復它。或者,您可以嘗試使用不同版本的 Moshi。另外,請參閱相關討論Moshi @Json annotation not working for com.github.kittinunf.fuel.moshi.moshiDeserializerOf?
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/531209.html
標籤:安卓科特林
上一篇:ValueError:如果您的資料具有單個特征,則使用array.reshape(-1,1)重塑您的資料,如果它包含單個樣本,則使用array.reshape(1,-1)
