multiple InsertJSONStructure

Just starting out? Need help? Post your questions and find answers here.
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

multiple InsertJSONStructure

Post by William Van Hoecke »

Hello,
I need to create a json string to send to my server. I have several structures and structured arrays.
I am however not sure how to use the INSERT functions like InsertJSONStructure and InsertJSONArray
I would expecty below code to produce something like
{
"Profile":{"naam":"aaa","straat":"bbb","gemeente":"ccc"},
"Client":{"naam":"ddd","straat":"eee","gemeente":"fff"},
"Licence":[
{"id":"person aaa","transactionid":"TR_1","serial":"ABC"},
{"id":"person bbb","transactionid":"TR_2","serial":"DEF"},
{"id":"person ccc","transactionid":"TR_3","serial":"GHI"}
]
}


but i always ends up with only the last structure in the composed json
{"naam":"ddd","straat":"eee","gemeente":"fff"}

What am I doing wrong here ?

Code: Select all

Structure AdresStruct
  naam.s
  straat.s
  gemeente.s
EndStructure

Structure LicenceStruct
  transactionid.s 
  id.s
  serial.s
EndStructure

Global Client.AdresStruct
Global Profile.AdresStruct
Global Dim Licence.LicenceStruct(10)

Licence(0)\id = "person aaa"
Licence(0)\transactionid = "TR_1"
Licence(0)\serial = "ABC"

Licence(1)\id = "person bbb"
Licence(1)\transactionid = "TR_2"
Licence(1)\serial = "DEF"

Licence(2)\id = "person ccc"
Licence(2)\transactionid = "TR_3"
Licence(2)\serial = "GHI"

Client\naam = "aaa"
Client\straat = "bbb"
Client\gemeente = "ccc"

Profile\naam = "ddd"
Profile\straat = "eee"
Profile\gemeente = "fff"

If CreateJSON(0)
    InsertJSONArray(JSONValue(0),Licence())
    InsertJSONStructure(JSONValue(0),@Klant.AdresStruct,AdresStruct)
    InsertJSONStructure(JSONValue(0),@Profile.AdresStruct,AdresStruct)
    retval.s = ComposeJSON(0)
    FreeJSON(0)
    MessageRequester(retval)
Else
    MessageRequester("Klant/profile/licence parse to json failed")
EndIf
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: multiple InsertJSONStructure

Post by Peter »

You must combine your structures into an upper structure (here sItem):

Code: Select all

EnableExplicit

Structure AdresStruct
  naam.s
  straat.s
  gemeente.s
EndStructure

Structure LicenceStruct
  transactionid.s 
  id.s
  serial.s
EndStructure

Structure sItem
  Client.AdresStruct
  Profile.AdresStruct
  Array Licence.LicenceStruct(10)
EndStructure

Define Item.sItem

Item\Licence(0)\id = "person aaa"
Item\Licence(0)\transactionid = "TR_1"
Item\Licence(0)\serial = "ABC"

Item\Licence(1)\id = "person bbb"
Item\Licence(1)\transactionid = "TR_2"
Item\Licence(1)\serial = "DEF"

Item\Licence(2)\id = "person ccc"
Item\Licence(2)\transactionid = "TR_3"
Item\Licence(2)\serial = "GHI"

Item\Client\naam = "aaa"
Item\Client\straat = "bbb"
Item\Client\gemeente = "ccc"

Item\Profile\naam = "ddd"
Item\Profile\straat = "eee"
Item\Profile\gemeente = "fff"

If CreateJSON(0)
  InsertJSONStructure(JSONValue(0),@Item, sItem)
  Define retval.s = ComposeJSON(0)
  FreeJSON(0)
  MessageRequester(retval)
Else
  MessageRequester("Klant/profile/licence parse to json failed")
EndIf
User avatar
William Van Hoecke
Posts: 50
Joined: Tue Oct 22, 2019 12:09 pm

Re: multiple InsertJSONStructure

Post by William Van Hoecke »

Thanks Peter,
I thought of that too, but then I will have to rename ALL my variables, In my real case a lot of large structs troughout a lot of code.

Thanks a lot
Post Reply