Android Question Complex Json Generator

victormedranop

Well-Known Member
Licensed User
Longtime User
HI, I use JSONGenerator to generate simple json string to consume some web services.
but now I need to generate a more complex string.

I user this code for generate I simple string

B4X:
Dim Map1 As Map
 Map1.Initialize
 Map1.put("RetailId",global_variables.reatilid)
 Map1.put("TerminalId",global_variables.terminalid)
 Map1.put("SerialTF",global_variables.XSerial)
 Map1.put("ValidationCode",global_variables.XSerial)
 Dim Data1 As List
 Data1.Initialize
 Data1.add(Map1)
 Dim JSONGenerator As JSONGenerator
 JSONGenerator.Initialize2(Data1)
 Msgbox(JSONGenerator.ToPrettyString(2), "")

now I need to generate more complex structure like this.

B4X:
{
  "glossary": {
  "title": "example glossary",
       "GlossDiv": {
              "title": "S",
                  "GlossList": {
                       "GlossEntry": {
                               "ID": "SGML",
                               "SortAs": "SGML",
                                "GlossTerm": "Standard Generalized Markup Language",
                                "Acronym": "SGML",
                               "Abbrev": "ISO 8879:1986",
          "GlossDef": {
                       "para": "A meta-markup language, used to create markup languages such as DocBook.",
                       "GlossSeeAlso": ["GML", "XML"]
                      },
           "GlossSee": "markup"
  }
  }
  }
  }
}

if some one can point me in the right direction.

Thanks.

Victor
 

DonManfred

Expert
Licensed User
Longtime User
now I need to generate more complex structure like this.
what did you tried so far?
It is not our work to code for you....

Create a map with
glossary and
title

Put another map into
GlossDiv which contain
title and another map into GlossList.
and so on...
{} single value
[] List
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
You are right. Sorry.:(

but if someone need help with this problem use CreateMap

like this

B4X:
Dim complex_MAP As Map = CreateMap("detalle": Array ("XXX01:01","XX02:"&"03x05-04x06-10","XXX03:"&"50","XXX04:01"))

then insert into the list.

Victor
 
Last edited:
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
I found a better way to create complex json structure using smart literal.
this simple code.

note valor1-6 at variables.

B4X:
$"{"RetailId":"${valor1}","TerminalId":"${valor2}","detalle":[{ "Loteria":"${valor3}","Jugada":"${valor4}","Monto":"${valor5}","Sorteo":"${valor6}" }]}"$

generate this valid json string

B4X:
{"RetailId":"Que chulo 1","TerminalId":"Que chulo 2","detalle":[{ "Loteria":"Que chulo 3","Jugada":"Que chulo 4","Monto":"Que chulo 5","Sorteo":"Que chulo 6" }]}


Victor
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You code will fail if one of the values includes quotes.

It is always better to use JSON generator to generate json:
B4X:
Dim m As Map = CreateMap("RetailId": valor1, ,"TerminalId": valor2, _
      "detalle": Array (CreateMap("Loteria": valor3, ,"Jugada": valor4, "Monto": valor5, "Sorteo": valor6)))
 
Upvote 0
Top