Android Question [SOLVED]Custom Type To JSON String

Jorge M A

Well-Known Member
Licensed User
Hello Everybody,
Maybe it's a silly question, but I'm new in B4A.
Is there a simple way to generate a JSON string from a Custom Type?

<code>
Type VehiculoHeader (Id_Vehiculo As Int, _
Placa As String, _
Marca As String , _
Submarca As String, _
Modelo As String, _
Fecha_Alta As String )

Dim veh as VehiculoHeader
(fill veh)...

jsonString= ????

</code>

Thanks in advanced.
 

Widget

Well-Known Member
Licensed User
Longtime User
Hello Everybody,
Maybe it's a silly question, but I'm new in B4A.
Is there a simple way to generate a JSON string from a Custom Type?

<code>
Type VehiculoHeader (Id_Vehiculo As Int, _
Placa As String, _
Marca As String , _
Submarca As String, _
Modelo As String, _
Fecha_Alta As String )

Dim veh as VehiculoHeader
(fill veh)...

jsonString= ????

</code>

Thanks in advanced.

I would create a class "VehiculoHeader" instead of the type and not use the type at all. The class would store all of its properties in a mapProperties variable and use subs to get/set values to the map. So you would have:
B4X:
'Class VechicloHeader
public fProperties as Map

public sub getIdVehiculo as Int
  return fProperties.GetDefault("VechicloHeader",0)
end sub
public sub setIdVehiculo(aValue as Int)
  fProperties.Put("VechicloHeader", aValue)
end sub

public sub Initialize
  'You could initialize all of the map properties here if you want default values
end sub
etc.

'Main code
public VHdr as VehiculoHeader
VHdr.Initialize
VHdr.IdVehifulo = 123
VHdr.Placa = "abcd"
'You can now reference VHdr.fProperties which contains all of the properties for VHDR and easily export them or convert them to JSON

The advantage is that it will be compatible with B4i and B4j. If you are only using B4A then one of the other solutions may be simpler.
 
Upvote 0
Top