Reputation: 1609
I have an iterable of People
that I save as a string
after converting from json. I want to know how would I convert the string
back to a list.
// Save data
val peopleString = myList.toString()
// String saved is
[People(name=john, age=23), People(name=mary, age=21), People(name=george, age=11)]
Now is it possible to convert peopleString
back to a list?
val peopleList: List<People> = peopleString.?
Upvotes: 1
Views: 546
Reputation: 25810
In short, no... kind of.
Your output is not JSON, and toString()
is the wrong function to use if you wanted JSON. The output of toString()
is not a proper serialization format that can be understood and used to rebuild the original data structure.
Converting a data structure into some format so that it can be transmitted and later rebuilt is known as serialization. Kotlin has a serializer which can serialize objects into a number of different formats, including JSON: https://github.com/Kotlin/kotlinx.serialization#quick-example.
It's not as easy to use as toString()
, but that's to be expected as toStrings
's purpose is very different from serialization.
Upvotes: 2