JSON Parser column re-order

jorvart

New Member
Licensed User
Longtime User
Hi, I discovered a curious problem/bug.

The following few lines of codes should illustrate it:

Dim parser As JSONParser
Response=Job.GetString
parser.Initialize(response)
Log (response)
Log(parser)

Dim trows As List
trows = parser.NextArray
Log(trows)

In this case Resonse vil be a single line of data from a SQL Server table.

The resulting log output from this is:

[{"Fornavn":"Arne","Etternavn":"Husveg","AnsattNr":"105"}]

(JSONTokener) at character 0 of [{"Fornavn":"Arne","Etternavn":"Husveg","AnsattNr":"105"}]

(ArrayList) [{Etternavn=Husveg, Fornavn=Arne, AnsattNr=105}]


So: After Parser.NextArray, the list contain the columns in the order Etternavn,Fornavn,AnsattNr in stead of the original Fornavn,Etternavn,AnsattNr ...!

I am not able to determin any pattern in how this column reordering takes place, even if it seemes to be consistent within one developent session.

If i later would reference this columns by name, there would be no problem. But I am trying to build a dynamic solution that can return any number of columns, so I need to get values by column number using a Map variable.


Regards Jorvart
 

jorvart

New Member
Licensed User
Longtime User
Yes but..

Hi Erel, and thank you for the very fast response :)

But maybe I am missing somthing. I am aware that the list vil contain a single item (of maps). What I dont get is why the map itself does not maintain the same order of the columns given to the parser..?

This would have made my life so much easier.

As it is now I will need to use the keys for getting the columns in the order i want them. This seems to be a bit of overkill as the original JSON expression has this information.
 

jorvart

New Member
Licensed User
Longtime User
Maps (hashtables) are usually not ordered in any specific way.

Accessing the values based on the keys has several advantages. One of these advantages is that the parser will continue to work even if a new field is added in the future.

Anyway this is how the native JSON parser works.

Ok Erel,
Can not let realities in life stop progress :)

Fixed it by letting my asp server return a list of colomns in addition to the data.
 
Top