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 ...
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?
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: