JsonGenerator

1
0
-1

Hi, I am using the JsonGenerator and I am having trouble getting it formatted correctly with my arrays - square brackets are missing

This is what I am trying to create

{
    "userId": "adsf1234",
    "dueDate": "2018/02/21",
    "items": [
        {
            "sku": "shirt",
            "id": 234567,
            "extras": [
                {
                    "type": "silk",
                    "price": 2.00,
                }
            ],
            "notes": "",
        },
        {
            "sku": "shirt",
            "id": 234568,
            "notes": "",
        },
        {
    ]
}

********* this is my code - 

ll_childobect = lnv_JsonGenerator.CreateJsonObject()

lnv_JsonGenerator.AddItemString(ll_ChildObject, "userId", 'W2DBZqIkce')
lnv_JsonGenerator.AddItemString(ll_ChildObject, "dueDate", "14/06/2018")

ll_item_array=lnv_JsonGenerator.additemobject(ll_childobject,"items")

lnv_JsonGenerator.AddItemString(ll_item_array, "sku", "shirt")
lnv_JsonGenerator.AddItemString(ll_item_array, "description", 'white shirt')

********* this is what I get

{
"userId":"W2DBZqIkce",
"dueDate":"14/06/2018",
"items":
      {
      "sku":"shirt",
      "description":"white_shirt"
      }
}
 
***** the square brackets are missing
 
 
** also is there any more documentation or example of using the generator 

 

Question Tags: 

Answers

Govinda Lopez answered JsonGenerator

1
0
-1

Hi Andrew,

 

Please try the AddItemArray function. 

 

This example creates an object root item and adds an array child item. The result is {"id":[101,102,103]}.

JsonGenerator lnv_JsonGenerator
lnv_JsonGenerator = Create JsonGenerator

// Create an object root item
Long ll_RootObject = lnv_JsonGenerator.CreateJsonObject()

// Add an array child item
Long ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootObject, "id")
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 101)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 102)
lnv_JsonGenerator.AddItemNumber(ll_ChildArray, 103)

You may find more about this here (page 305). Or you can also download an example app that uses the the JSonGenerator object here.

 

 

Regards,

Andrew Davis's picture

I tried this previously - and i can add this however in my requirement the array needs to have 

in your example "id": [ "key":"string", "key2","string2"] .....

if i try to add object it just doesnt add