I originally discovered this problem in B4J and reproduced in B4A.
Steps to reproduce the problem: Run the included code in either B4A or B4J.
test example:
'capitalized keyname: Test
Dim strJson As String=$"[{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","Test":"1"},
{"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","Test":"1"}]"$
test(strJson) 'Test=null while 1 is expected
'lowercase key name: test
strJson=$"[{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","test":"1"},
{"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","test":"1"}]"$
test(strJson) 'Test=1 as expected
B4X:
Private Sub test(strJson As String)
Dim parser As JSONParser
'parser.Initialize($"${J.GetString}"$)
parser.Initialize($"${strJson}"$)
LogDebug("strJson=" & strJson)
Dim lstResult As List
lstResult.Initialize
Try
lstResult=parser.NextArray
Catch
Log(LastException)
Return
End Try
For Each m As Map In lstResult 'ignore
'go throught each record
'''''''
Log("***: " & m & " ***")
Dim msg As String=$"orderItem_id=${m.Get("orderitem_id")}"$
Log(msg)
msg=$"qty=${m.Get("qty")}"$
Log(msg)
msg=$"amt=${m.Get("amt")}"$
Log(msg)
msg=$"Test=${m.Get("test")}"$
Log(msg)
Next
End Sub
It works if the key is in lowercase and I have to use lowercase key ("test", not "Test") to be able to retrieve the test value in question.
For json string containing "Test":1, m.get.("Test") returns null while 1 is expected. something wrong here.
For json string containing "test":1, m.get.("test") returns 1 as expected.
Dim strJson As String
strJson=$"[{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","Test":"1"},
{"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","Test":"1"},
{"orderitem_id":"1","order_id":"1","qty":"1","amt":"15","test":"1"},
{"orderitem_id":"2","order_id":"1","qty":"2","amt":"20","test":"1"}]"$
Log("keyname: Test or test")
For Each m As Map In strJson.As(JSON).ToList
Log(IIf(m.ContainsKey("test"), m.Get("test"), m.Get("Test")))
Next
or GetDefault
B4X:
Log("keyname: Test or test")
For Each m As Map In strJson.As(JSON).ToList
Log(m.GetDefault("test", m.Get("Test")))
Next