title: Using Gson in Kotlin to parse JSON array
body: <p>Trying to parse JSON array in Kotlin, made it work for single JSON object to a WeatherObject object (code snippet below) </p>
  
  <pre><code>{
  "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
  </code></pre>
  
  <p>}</p>
  
  <p>but not sure how to do the same if the JSON is a array of same JSON object, i.e.</p>
  
  <p>from json array [ {}, {} …]  to ArrayList&lt;WeatherObject &gt;</p>
  
  <p>something like:</p>
  
  <pre><code>fun getWeatherObjectArrayFromJson(jsonStr: String): ArrayList&amp;lt;WeatherObject &amp;gt
  </code></pre>
  
  <p>having problem with gsonBuilder.registerTypeAdapter(ArrayList&lt;WeatherObject &amp;gt::class.java, WeatherDeserializer())</p>
  
  <pre><code>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&lt;WeatherObject&gt; {
  
      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
      }
  </code></pre>
  
  <p>Update:</p>
  
  <p>tried the solution from chandil03, IT IS WORKING! put the testing json array data and the function here:</p>
  
  <p>tried</p>
  
  <pre><code>fun getWeatherObjectFromJsonArray(jsonArrayStr: String): List&lt;WeatherObject&gt; {
  
          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&lt;WeatherObject&gt; = gson.fromJson(stringReader , Array&lt;WeatherObject&gt;::class.java).toList()
          //val weatherList: List&lt;WeatherObject&gt; = gson.fromJson(jsonReader, Array&lt;WeatherObject&gt;::class.java).toList
  
          return weatherList
      }
  </code></pre>
  
  <p>got exception at</p>
  
  <pre><code>val weatherList: List&lt;WeatherObject&gt; = gson.fromJson(stringReader , Array&lt;WeatherObject&gt;::class.java).toList()
  
  com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  </code></pre>
  
  <p>the json array data is like:</p>
  
  <pre><code>[
  {
    "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
  }
  ]
  </code></pre>