JSonGenerator

COBRASoft

Active Member
Licensed User
Longtime User
Hi,

I need the JSonGenerator to produce JSon like shown below. How can I achieve these examples with list/map/type? I sometimes need 5 or 6 variables within a record, so I though a type would be perfect for this. It would be nice to have a JSonBuilder object :).

1st example:
{"Key":"Value","Key2":"Value2"}

2nd example:
[{"Record":"1", "Key":"Value1"},{"Record":"2", "Key":"Value2"}]

3rd example:
[{"Records":
[{"Record":"1","Key":"Value1"},
{"Record":"2","Key":"Value2"}],
"ResponseTime":"\/Date(123)\/"
}]

Greetings,
Sigurd
 

COBRASoft

Active Member
Licensed User
Longtime User
Hi,

I saw that tutorial... There's no explanation to achieve what I ask in my examples. It shows how to read an existing JSon and how to add that JSon later on. It doesn't show how to build the JSon from scratch in B4A :).

Greetings,
Sigurd
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim gen As JSONGenerator
   '1
   Dim m As Map
   m.Initialize
   m.Put("Key", "Value")
   m.Put("Key2", "Value2")
   gen.Initialize(m)
   Msgbox(gen.ToPrettyString(4), "Example 1")
   
   '2
   Dim List As List
   List.Initialize
   Dim m1, m2 As Map
   m1.Initialize : m2.Initialize
   m1.Put("Record", "1")
   m1.Put("Key", "Value1")
   m2.Put("Record", "2")
   m2.Put("Key", "Value2")
   List.Add(m1)
   List.Add(m2)
   gen.Initialize2(List)
   Msgbox(gen.ToPrettyString(4), "Example 2")
   
   '3
   Dim List1, List2 As List
   Dim m1, m2, m3 As Map
   List1.Initialize : List2.Initialize
   m1.Initialize : m2.Initialize: m3.Initialize
   m1.Put("Records", List2)
   m2.Put("Key", "Value1")
   m2.Put("Record", "1")
   m3.Put("Key", "Value2")
   m3.Put("Record", "2")
   List2.Add(m2)
   List2.Add(m3)
   m1.Put("ResponseTime", "/Date(123)/")
   List1.Add(m1)
   gen.Initialize2(List1)
   Msgbox(gen.ToPrettyString(4), "Example 3")
End Sub
 
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
Thx Erel, I'll try it later today. I forgot you can put multiple items to the same map :).

Is there a chance you'll have a real date type? E.g. this is required to form the dates accordingly for the JSonGenerator. Also for datetime formatting, this would be handy.
 
Last edited:
Upvote 0

COBRASoft

Active Member
Licensed User
Longtime User
Map and List working perfectly!

My dates '/Date(123456...)/' are not including the timeoffset (+1 here in belgium). Does somebody know how to correct this?
 
Upvote 0
Top