B4J Question For Each statement difference between B4J and B4A?

PaulMeuris

Active Member
Licensed User
Hi,
I am working on a B4X app and there seems to be a difference in the result after compilation with B4J and B4A.
The source code is in both cases the same (in B4XMainPage).
Here is the code snippet in question:
getting column headers:
            Dim hoofd As Map = parser.NextObject
            Dim items As List = hoofd.Get("categories")
            For i = 0 To items.Size-1
                Dim rec As Map = items.Get(i)
                If i = 0 Then
                    sb.Append("<tr>").Append(CRLF)
                    For Each key As String In rec.Keys
                        Log("key = " & key)
                        sb.Append("<th>").Append(key).Append("</th>")
                    Next
                    sb.Append("</tr>").Append(CRLF)
                End If
In B4J the logs give the following result:
1617902100415.png

In B4A the logs give the following result:
1617902166724.png

The data comes from an online database and is in both cases the same (JSON string).
Why is the order of the column headers different?

Greetings,
Paul.
 

Sandman

Expert
Licensed User
Longtime User
There are very rare cases where the order is important.
I would go even further and say that if the order in a map is important, then that map is poorly designed.

(I'm willing to listen if somebody wants to change my mind.)
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I would go even further and say that if the order in a map is important, then that map is poorly designed.
In JSON, if you want to preserve the order, one should use an array (so yes, if order is important, it is not a very well designed JSON structure)
 
Upvote 0

PaulMeuris

Active Member
Licensed User
Thank you Erel and Sandman for your quick reply. I tried a different approach...
A JSON object can consist of an object of objects OR an array of arrays.
Using indexed arrays (and not associative arrays or key/value maps) to produce a JSON object results in a consistent way to order the columns.
An example:
JSON object with an array of arrays:
        Log(j.GetString)
        Dim parser As JSONParser
        parser.Initialize(j.GetString)
        Dim hoofd As List = parser.NextArray
        Dim lst As List = hoofd.Get(0)
        Log(lst)
        sb.Append("<tr>").Append(CRLF)
        For i = 0 To lst.Size - 1
            sb.Append("<th>").Append(lst.Get(i)).Append("</th>")
        Next
        sb.Append("</tr>").Append(CRLF)
        sb.Append("<tr>")
        For i = 1 To hoofd.Size - 1
            Dim rec As List = hoofd.Get(i)
            Log(rec)
            For x = 0 To rec.Size - 1
                sb.Append("<td>").Append("<a href='http://").Append(rec.Get(x)).Append(".").Append("categories").Append(".local'>").Append(rec.Get(x)).Append("</a>").Append("</td>")               
            Next
            sb.Append("</tr>").Append(CRLF)
        Next
And the logs show the same results...
B4J log:
1617980191533.png

B4A log:
1617980258627.png

And the result in a webview:
1617980322770.png

So in my future apps I will go with this approach...
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I would go even further and say that if the order in a map is important, then that map is poorly designed.

(I'm willing to listen if somebody wants to change my mind.)
The order is not important for machine to read, I mean json parser but I found it is better for my eyes as a human to expect a consistent format.
 
Upvote 0
Top