Arraylist from json can not be iterated


i created a ArrayList with PersonDto objects and transform it to a json string on the server to send it to the client. in the client i try to parse this string back to the ArrayList of PersonDto objects but only get an Array back

val listPerson = JSON.parse<ArrayList<Person>>(jsonPersonList)    
// creates following error
(anonymous function)	Uncaught TypeError: listPerson.get_za3lpa$ is not a function

if i change the code to this code, it works

val listPerson = JSON.parse<Array<Person>>(data)
val person = listPerson[0]

is there any possibilty to pass a list from the client to the server using json?


Assuming that ‘data’ is the same as ‘jsonPersonList’, JSON doesn’t have a representation that would distinguish a list from an array. To JSON, it’s all arrays.
So what you would need, is a way for the parser to parse an array into a list instead.

Is there a reason why you need the data in a list, rather than an array?

Cannot you use something like:

val listPerson = JSON.parse<Array<Person>>(data).toList()



ok, using toList() works, the only problem is that if i have a dto with lists inside, i have to do this for all lists inside the dto, what makes it more complicated than working with for example Gson


Is there a reason why you can’t work with Arrays? Any APIs that only work with Lists? Perhaps they can be converted as-needed.

Less convenient indeed, but I have no experience with Kotlin/JS, only Kotlin/JVM and use JacksonJson mapping w/Spring, so it uses introspection to figure out the right conversion.


no there is no special reason. i work with lists on the server side and have to work with arrays on the client side, i was hoping that i could use a more similar code for the server and client.


actually the problem seems to be deeper, when i try to get a property of an object which is a list. i have a InitDto with a list of PersonDto objects, which does not work. no matter if i call this line

// list with 6 person objects inside
var person = initDto.listPerson[0]

or this line

var person = initDto.listPerson.get(0)

i always get this error:

main$lambda	(anonymous function)	Uncaught TypeError: initDto.personList.get_za3lpa$ is not a function

and when i try to iterate the list like this:

 initDto.personList.forEach { person ->

i get this error:

main$lambda	(anonymous function)	Uncaught TypeError: initDto.personList.iterator is not a function


i found an extremely ugly workaround. if i add following lines:

val sPersonList = JSON.stringify(initDto.personList)
val personList = JSON.parse<Array<Person>>(sPersonList)

then i can iterate the list of person objects

 personList.forEach { person ->
     // do someting