0

I need to gather some data from an API from my Kotlin app and I need to parse a JSONs array from JS in a Kotlin array. This is the code that I'm using to do request to the API:

URL(apiUrl).readText()

And the output is something like this:

[{"id":5,"name":"Test1","ip":"192.168.0.1","port":10302,"id_slave":1},{"id":6,"name":"Test2","ip":"192.168.1.1","port":502,"id_slave":1}]

There's anyway to parse that string as a Kotlin array?

1 Answers1

0

You output seems like it is a JSONArray. You can use Gson library to change your JSONArray to Kotlin Array.

implement gson library in gradel:

implementation 'com.google.code.gson:gson:2.8.5'

kotlin code:

val gson = GsonBuilder().create()
val ModelArray= gson.fromJson(body,Array<YourDataModel>::class.java).toList()
Nabin
  • 166
  • 3
  • 11