2
試圖科特林解析JSON陣列,使之成爲單JSON對象到WeatherObject對象(下面的代碼片段)在科特林使用GSON解析JSON數組
{
"coord": {
"lon": -2.93,
"lat": 43.26
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}],
"main": {
"temp": 283.681,
"temp_min": 283.681,
"temp_max": 283.681,
"pressure": 991.72,
"sea_level": 1034.92,
"grnd_leve": 991.72,
"humidity": 98
},
"wind": {
"speed": 1.07,
"deg": 144.001
},
"dt": 1429773245,
"id": 3128026,
"name": "Bilbao",
"cod": 200
}
,但不知道工作如何這樣做,如果JSON是相同JSON對象的陣列,即
從JSON數組[{},{} ...]到ArrayList的<WeatherObject>
類似:
fun getWeatherObjectArrayFromJson(jsonStr: String): ArrayList<WeatherObject >
與gsonBuilder.registerTypeAdapter具有問題(ArrayList的< WeatherObject & GT :: class.java,WeatherDeserializer())
class WeatherObject {
var main: String = ""
var description: String = ""
var temp: Float = 0.0f
var tempMax: Float = 0.0f
var tempMin: Float = 0.0f
var humidity: Int = 0
var wind: WindObject? = null
}
class WeatherDeserializer : JsonDeserializer<WeatherObject> {
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): WeatherObject? {
val jsonObj = json as JsonObject
val wheather = WeatherObject()
val wind = WindObject()
val jsonWeatherArray = jsonObj.getAsJsonArray("weather").get(0)
val jsonMainObj = jsonObj.getAsJsonObject("main")
val jsonWindObj = jsonObj.getAsJsonObject("wind")
wheather.main = jsonWeatherArray.asJsonObject.get("main").asString
wheather.description = jsonWeatherArray.asJsonObject.get("description").asString
wheather.temp = jsonMainObj.get("temp").asFloat
wheather.tempMax = jsonMainObj.get("temp_max").asFloat
wheather.tempMin = jsonMainObj.get("temp_min").asFloat
wheather.humidity = jsonMainObj.get("humidity").asInt
wind.speed = jsonWindObj.get("speed").asFloat
wind.deg = jsonWindObj.get("deg").asFloat
wheather.wind = wind
return wheather
}
}
fun getWeatherObjectFromJson(jsonStr: String): WeatherObject {
var stringReader: StringReader = StringReader(jsonStr)
var jsonReader: JsonReader = JsonReader(stringReader)
val gsonBuilder = GsonBuilder().serializeNulls()
gsonBuilder.registerTypeAdapter(WeatherObject::class.java, WeatherDeserializer())
val gson = gsonBuilder.create()
val weather: WeatherObject = gson.fromJson(jsonReader, WeatherObject::class.java)
return weather
}
更新:
從試過的溶液chandil03,它正在工作!把測試JSON數組數據,這裏的功能:
試圖
fun getWeatherObjectFromJsonArray(jsonArrayStr: String): List<WeatherObject> {
var stringReader: StringReader = StringReader(jsonStr)
//var jsonReader: JsonReader = JsonReader(stringReader)
val gsonBuilder = GsonBuilder().serializeNulls()
gsonBuilder.registerTypeAdapter(WeatherObject::class.java, WeatherDeserializer())
val gson = gsonBuilder.create()
val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList()
//val weatherList: List<WeatherObject> = gson.fromJson(jsonReader, Array<WeatherObject>::class.java).toList
return weatherList
}
得到了異常的
val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList()
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
JSON數組數據是這樣的:
[
{
"coord": {
"lon": -2.93,
"lat": 43.26
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}],
"main": {
"temp": 283.681,
"temp_min": 283.681,
"temp_max": 283.681,
"pressure": 991.72,
"sea_level": 1034.92,
"grnd_leve": 991.72,
"humidity": 98
},
"wind": {
"speed": 1.07,
"deg": 144.001
},
"clouds": {
"all": 36
},
"dt": 1429773245,
"id": 3128026,
"name": "Bilbao",
"cod": 200
},
{
"coord": {
"lon": -2.93,
"lat": 43.26
},
"weather": [{
"id": 802,
"main": "Clouds",
"description": "scattered clouds",
"icon": "03d"
}],
"main": {
"temp": 283.681,
"temp_min": 283.681,
"temp_max": 283.681,
"pressure": 991.72,
"sea_level": 1034.92,
"grnd_leve": 991.72,
"humidity": 98
},
"wind": {
"speed": 1.07,
"deg": 144.001
},
"clouds": {
"all": 36
},
"dt": 1429773245,
"id": 3128026,
"name": "Bilbao",
"cod": 200
}
]
感謝chandil03!但有WeatherDeserializer類的問題:JsonDeserializer,它處理單個json對象,它如何處理具有相同json對象的數組的json字符串,是否需要定義解串器的數組版本? –
lannyf
@lannyf不,上面提到的代碼只是包含包含天氣對象數組的字符串。我已經檢查過了。只有一行需要更改。 – chandil03
嘗試過你的功能,它得到了異常。我更新了問題中的函數和json數組dat。 – lannyf