Gson is giving wrong name to json value

Hi, I’m trying to convert ArrayList with GSON to JSON. I have a json as a result, but not the json I expected. I need this to use Fuel.post

val requestParams = ArrayList<Pair<String, Any>>()
val coordonnee = requestParams.add(Pair(“coordonnees”, Singleton.instance.coordonnee!!.toParam()))
val details = requestParams.add(Pair(“details”, result))
val price = requestParams.add(Pair(“price”, price.toString()))

println(“requestParams : $requestParams”)

val gson = GsonBuilder().setPrettyPrinting().create()
val json: String = gson.toJson(requestParams)

My array :

[
(coordonnees,
[(genre, M.), (nom, nom), (adresse, adresse), (ville, ville), (cp, 45460), (email, test@test.fr), (tel, 0611223344)]
),
(details,
“value”
),
(price, 0)
]

The json I get. Why do I have “first” and “second” all the time ?

{
“first”: “coordonnees”,
“second”: [{
“first”: “genre”,
“second”: “M.”
},
{
“first”: “nom”,
“second”: “nom”
},
{
“first”: “adresse”,
“second”: “adresse”
},
{
“first”: “ville”,
“second”: “ville”
},
{
“first”: “cp”,
“second”: “45”
},
{
“first”: “email”,
“second”: “test@test.fr
},
{
“first”: “tel”,
“second”: “0677039672”
}
]
}, {
“first”: “details”,
“second”: “value”
}, {
“first”: “price”,
“second”: “0”
}]

Because you are using Pair where the 2 items in the the pair are in properties named first and second.

Instead of Pair you probably want to create maps which GSON handles as objects.

1 Like

Yes, I’ve doing this right before you said it, it works perfectly. Thanks !