Subject Re: JSON Objects in Array
From Adrian B <a@x.net>
Date Mon, 3 Feb 2020 10:43:55 -0500
Newsgroups xb2net


First off, let's format the JSON string so that we can visualize the
structure of the JSON object we need to create:

{
    "syncData": {
       "sd_products": {
          "serverData":"true",
          "selectLimit": 3 },
       "sd_customers": {
          "serverData":"true",
          "selectLimit": 3}
    }
}

When creating JSON objects in Xb2NET, the method :AddVar(cName, xValue)
will add a key-value pair to the JSON object. I see you are doing the
correct steps where you add the key-value pairs to your oProductsPos and
oCustomersPos objects, but you then add them to a single array
aSyncDataPos. The value of the "syncData" key is a JSON object and
therefore should be added to oJSONSyncData like so:

        oJSONSyncData:AddVar("sd_products", oProductsPos)
        oJSONSyncData:AddVar("sd_customers", oCustomersPos)

oJSONSyncData now holds the 'value' for the "syncData" key

Lastly, we need to create the 'master' JSON with the key-value pair
{"syncData":JSON Object}

        oMaster := xbJSON():new()
        oMaster:AddVar("syncData", oJSONSyncData)


I've created the following code that generates the JSON string you've
provided:

// initialize all necessary json objects
oMaster := xbJSON():new()
oSync := xbJSON():new()    // value of 'syncData' key
oProd := xbJSON():new()    // value of 'sd_products' key
oCust := xbJSON():new()    // value of 'sd_customers' key

// build sd_products; add it to syncData value
oProd:AddVar("serverData","true")
oProd:AddVar("selectLimit",3)
oSync:AddVar("sd_products". oProd)

// build sd_customers; add it to syncData value
oCust:AddVar("serverData","true")
oCust:AddVar("selectLimit",3)
oSync:AddVar("sd_customers",oCust)

// create the single key-value pair
oMaster:AddVar("syncData", oSync)

? oMaster:AsString()



On 2/3/2020 4:01 AM, Rudolf Israel wrote:
> Hello Boris
>
>
> I would like to create the following JSON string for the transfer to the server:
>
> {
> "syncData":{
> "sd_products":{
> "serverData":"true",
> "selectLimit": 3
> },
> "sd_customers":{
> "serverData":"true",
> "selectLimit": 3
> }
> }
> }
>
>
> I created the following source code with XB2Net:
>
> oJSONSyncData := xbJSON():new()
> oJSONProducts := xbJSON():new()
> oJSONCustomers := xbJSON():new()
> aSyncDataPos := {}
>
> oProductsPos := xbJSON():new()
> oProductsPos:AddVar("serverData","true")
> oProductsPos:AddVar("selectLimit",3)
> oJSONProducts:AddVar("sd_products",oProductsPos)
> aadd(aSyncDataPos,oJSONProducts)
>
> oCustomersPos := xbJSON():new()
> oCustomersPos:AddVar("serverData","true")
> oCustomersPos:AddVar("selectLimit",3)
> oJSONCustomers:AddVar("sd_customers",oCustomersPos)
> aadd(aSyncDataPos,oJSONCustomers)
>
> oJSONSyncData:AddVar("syncData",aSyncDataPos)
>
>
> The result of what I then transfer to the server with the Execute method looks like this::
> {
> "syncData":[{
> "sd_products":{
> "serverData":"true",
> "selectLimit": 3}
> },
> {"sd_customers":{
> "serverData":"true",
> "selectLimit": 3
> }
> }
> ]}
>
>
> XB2Net therefore puts additional brackets around the array.
> As a result, the result from the server is incorrect or empty.
> What am I doing wrong ?
>
> Thanks in advance
>
> Rudi Israel
>


Recent messages in this thread
 
-# JSON Objects in Array Rudolf Israel 03-Feb-2020 04:01 am
.-# Re: JSON Objects in Array (Current message) Adrian B 03-Feb-2020 10:43 am
..|# Re: JSON Objects in Array Rudolf Israel 09-Feb-2020 07:35 am
..\# Re: JSON Objects in Array Rudolf Israel 09-Feb-2020 07:35 am