val object= string.trim().removeSurrounding("{", "}").split(",").map {
it.split(":")[1].trim()
}
val id = productString[0]
val price = productString[1]
val isAvailable= productString[2].toBooleanOrNull()
The problem is
price
. It cointains
,
between 29 and 99 and it makes error. How I can avoid dividing price on two parts in that case? I don't want to use
.
instead of
,
s
Steve Georgakis
10/27/2022, 9:13 AM
.
is the standard for decimals. if you really need to use
,
, then you should pass it as a string. also, a json parsing library would probably be more convienient
s
Sam
10/27/2022, 9:15 AM
Simple solution would be to change the first
split
from
split(",")
to
split(", ")
(with an additional space after the comma). That will work if you can be sure that commas between entries will always be followed by a space.
➕ 1
i
Ink
10/27/2022, 9:25 AM
Thank you all
e
ephemient
10/27/2022, 9:58 AM
or set up NumberFormat to parse it in a locale that uses comma as the decimal separator
j
Joffrey
10/27/2022, 9:59 AM
I concur with @Steve Georgakis, though. You should really consider a proper serialization format and corresponding library, if you're going to deal with several of these