B4J Question Building a Json Tree error

TomDuncan

Active Member
Licensed User
Hi All.

What I am trying to do is import the values from a Json file into a database, then as needed export the Json file in the same order. The software I am using only likes the Json in a specific order.

I am trying to build a json file from a database.

Here is the final code..

B4X:
    Dim final As Map
    final.Initialize
    final.Put("operators",operators)
    Log(final)
    Dim jgen As JSONGenerator
    jgen.Initialize(final) ' (CreateMap("operators": operators))
    Dim st As String = jgen.ToPrettyString(3)
    st = st.Replace($"\"$,"")
    st = st.Replace($""""$,$"""$)
    Log(st)

Here is a copy of the final map and the resultant code.
I have had to take out back slashes and extra double quotes. (no idea my error)

B4X:
Waiting for value (2 ms)
(MyMap) {operators={id_01={top=20, left=60, properties={title=Light Switch, inputs={}, outputs={"output_1"="On/Off"}}}, id_02={top=60, left=820, properties={title=Operator 2, inputs={"input_1"="In 1", "input_2"="In 2"}, outputs={}}}, id_03={top=220, left=260, properties={title=Operator 3, inputs={"input_1"="In 1"}, outputs={"output_1"="Out 1"}}}, id_04={top=140, left=560, properties={title=Operator 4, inputs={"input_1"="In 1"}, outputs={"output_1"="Out 1"}}}}}
{
   "operators": {
      "id_02": {
         "top": 60,
         "left": 820,
         "properties": {
            "outputs": {},
            "inputs": {
               "input_2": "In 2",
               "input_1": "In 1"
            },
            "title": "Operator 2"
         }
      },
      "id_03": {
         "top": 220,
         "left": 260,
         "properties": {
            "outputs": {
               "output_1": "Out 1"
            },
            "inputs": {
               "input_1": "In 1"
            },
            "title": "Operator 3"
         }
      },
      "id_01": {
         "top": 20,
         "left": 60,
         "properties": {
            "outputs": {
               "output_1": "On/Off"
            },
            "inputs": {},
            "title": "Light Switch"
         }
      },
      "id_04": {
         "top": 140,
         "left": 560,
         "properties": {
            "outputs": {
               "output_1": "Out 1"
            },
            "inputs": {
               "input_1": "In 1"
            },
            "title": "Operator 4"
         }
      }
   }
}

What happens is that the order of the json code output is not in the same order as the maps.
Things like "title" is the first under properties and the order of the id's is incorrect.

Any thoughts on what I am doing wrong.

Tom
 
Last edited:
Top