B4J Question Converting Map with number as key to JSON

Chris2

Active Member
Licensed User
After saving a map that has numbers for keys and values to a JSON string, the keys seem to be turned into strings, and when turning the json string back into a map I seem to have to treat the keys as strings, rather than the int they were originally. The values stay as numbers.

It makes more sense as code :)...
B4X:
    Dim key As Int = 7
    Dim m As Map = CreateMap(key: Array As Int(1, 2, 3))
 
    Dim jsonString As String = m.As(JSON).ToString
    Log(jsonString)
 
    'and back...
    Dim m2 As Map = jsonString.As(JSON).ToMap
    Log(m2)
 
    For Each k As Int In m2.Keys     'this loop fails to get the map values
        Dim l As List = m2.Get(k)
        Log(l)
'        For i=0 To l.Size-1            'generates error: java.lang.RuntimeException: Object should first be initialized (List).
'            Dim num As Int = l.Get(i)
'            Log(num)
'        Next
    Next
 
    For Each k2 As String In m2.Keys    'but this loop works 
        Dim l As List = m2.Get(k2)
        For j=0 To l.Size-1
            Dim num As Int = l.Get(j)
            Log(num)
        Next
    Next

B4X:
{
    "7": [                'note that the JSON conversion has turned 7 into "7"
        1,
        2,
        3
    ]
}
(MyMap) {7=[1, 2, 3]}
(List) Not initialized        'from line 13 - Log(l) in the  For Each k As Int In m2.Keys loop
1                                     'from line 24 - Log(num) in the  For Each k As String In m2.Keys loop
2
3

Am I doing something wrong here, or is it just the way JSON works?
Why does the key (7) get set as a string in the JSON when it was set as int before being converted to JSON?
 
Last edited:
Solution
JSON data is written as key/value pair. The keys must be strings, written with double quotes

In your example the call to .Tostring finds a numeric key 7, so it is converted to its string representation "7"

The reverse conversion (.ToMap) considers the keys to be strings, and the JSON key "7" is convert to B4x string type "7"

in your code when doing ' For Each k As Int In m2.Keys ' there is a conversion of string to int, but the entry in the map for int 7 doesn't exist so the the list obtains by m2.get(k) is undefined(not initialized) :
  • m2.get(7) => undefined
  • m2.get("7") = > defined

you can also not force a type for the key during the for each loop and let the automatic conversion by b4x be done.
The code...

Star-Dust

Expert
Licensed User
Longtime User
the key is int not string so it doesn't find it

Change this
B4X:
Dim l As List = m2.Get(k2.As(Int))
 
Upvote 0

Chris2

Active Member
Licensed User
Thanks, but I think you misunderstand the problem.

The second loop (For Each k2 As String In m2.Keys...) works fine.
It's the first one where I treat k as int that doesn't work.
 
Upvote 0

Quandalle

Member
Licensed User
JSON data is written as key/value pair. The keys must be strings, written with double quotes

In your example the call to .Tostring finds a numeric key 7, so it is converted to its string representation "7"

The reverse conversion (.ToMap) considers the keys to be strings, and the JSON key "7" is convert to B4x string type "7"

in your code when doing ' For Each k As Int In m2.Keys ' there is a conversion of string to int, but the entry in the map for int 7 doesn't exist so the the list obtains by m2.get(k) is undefined(not initialized) :
  • m2.get(7) => undefined
  • m2.get("7") = > defined

you can also not force a type for the key during the for each loop and let the automatic conversion by b4x be done.
The code becomes
B4X:
  Dim key As Int = 7
    Dim m As Map = CreateMap(key: Array As Int(1, 2, 3))
 
    Dim jsonString As String = m.As(JSON).ToString
    Log(jsonString)
 
    'and back...
    Dim m2 As Map = jsonString.As(JSON).ToMap
    Log(m2)

 
    For Each k  In m2.Keys
        Dim l As List = m2.Get(k)
       For j=0 To l.Size-1
          Dim num As Int = l.Get(j)
          Log(num)
        Next
    Next
 
Last edited:
Upvote 0
Solution

Chris2

Active Member
Licensed User
Many thanks @Quandalle.
JSON data is written as key/value pair. The keys must be strings, written with double quotes
That's the key statement I think (no pun intended). I didn't know that in JSON the key had to be a string.

you can also not force a type for the key during the for each loop and let the automatic conversion by b4x be done.
For Each k In m2.Keys...
I didn't know we could do that either!
 
Upvote 0
Top