在kotlin中使用Gson来解析json数组

试图解析kotlin中的json数组,使其适用于WeatherObject对象的单个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&lt;WeatherObject &gt 

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 } ] 

One Solution collect form web for “在kotlin中使用Gson来解析json数组”

您需要在fromJson()函数调用中更改参数,如下所示:

 val weatherList: List<WeatherObject> = gson.fromJson(stringReader , Array<WeatherObject>::class.java).toList() 

您需要为类类型传递Array<WeatherObject>::class.java ,然后将结果转换为List 。 无需更改registerTypeAdapter()函数调用。

检查以下代码:

 fun getWeatherObjectFromJson(jsonStr: 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() return weatherList } 
  • 如何创建像anko - DSL语法Kotlin嵌套函数回调
  • Android Studio中的R.drawable的访问会引发KotlinFrontEndException
  • Anko日志与详细和调试不起作用
  • 无法在终端中运行Kotlin程序
  • 简单的方法来找出一个类是匿名/对象,并从Kotlin实例中获取具体的类名
  • Kotlin自定义对话框指定为非null的参数
  • 为什么不安全的.run()调用在Kotlin的空值上工作正常?
  • 在这种情况下,神秘地转义try-catch块的例外最可能的原因是什么?
  • msg:在数据绑定中找不到属性“android:text”的值types为java.lang.String的getter?
  • 为什么公共内联函数可以调用私有构造函数?
  • Kotlin中两个具有不同类型参数的函数
  • Kotlin language will be the best programming language for Android.