1. Malek Taha
  2. PowerBuilder
  3. Wednesday, 1 February 2023 22:18 PM UTC

Hi

   I am building a JSON string using the jsongenerator object

I have a root item with a handle of ll_childobject

In a loop i am trying to create a new array called "measures" 

on the first iteration i get 45 for the value of ll_measuresArray and 46 for the value of ll_measuresObject  

On the second itetration i get again 45 for the ll_measuresArray and 75 for the value of ll_measuresObject

 

the array handle did not change not sure if this is a known issue or not

using PB2019R3 build Build2779

any help would be greately appreciated

Thanks

Malek

  

FOR ii = 1 TO ll_rowcount STEP 1

---------

ll_measuresArray = lnv_JsonGenerator.AddItemArray(ll_childobject, "measures")

-----

ll_measuresObject = lnv_JsonGenerator.AddItemObject(ll_measuresArray)

Andreas Mykonios Accepted Answer Pending Moderation
  1. Thursday, 2 February 2023 07:39 AM UTC
  2. PowerBuilder
  3. # 1

Hi.

The code you provide doesn't show the whole logic. I give you two examples:

1st.

Long ll_RootArray, ll_ChildArray
integer li_i, li_j
JsonGenerator lnv_JsonGenerator

lnv_JsonGenerator = Create JsonGenerator

// Create an array root item
ll_RootArray = lnv_JsonGenerator.CreateJsonArray()

for li_i = 1 to 3
	// Add an array to the root Array.
	ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootArray)
	for li_j = 1 to 5
		// Add an array child item
		lnv_JsonGenerator.AddItemNumber(ll_ChildArray, li_j)
	next 
next

messagebox("", lnv_JsonGenerator.getjsonstring( ))

if isvalid(lnv_JsonGenerator) then destroy lnv_JsonGenerator

The resulting json will be:

[
	[
		1,
		2,
		3,
		4,
		5
	],
	[
		1,
		2,
		3,
		4,
		5
	],
	[
		1,
		2,
		3,
		4,
		5
	]
]

2nd.

Long ll_RootArray, ll_ChildArray
integer li_i, li_j
JsonGenerator lnv_JsonGenerator

lnv_JsonGenerator = Create JsonGenerator

// Create an array root item
ll_RootArray = lnv_JsonGenerator.CreateJsonObject()

for li_i = 1 to 3
	// Add an array to the root Array.
	ll_ChildArray = lnv_JsonGenerator.AddItemArray(ll_RootArray, "MyNumbers" + string(li_i))
	for li_j = 1 to 5
		// Add an array child item
		lnv_JsonGenerator.AddItemNumber(ll_ChildArray, li_j)
	next 
next


messagebox("", lnv_JsonGenerator.getjsonstring( ))

if isvalid(lnv_JsonGenerator) then destroy lnv_JsonGenerator

The resulting json will be:

{
	"MyNumbers1": [
		1,
		2,
		3,
		4,
		5
	],
	"MyNumbers2": [
		1,
		2,
		3,
		4,
		5
	],
	"MyNumbers3": [
		1,
		2,
		3,
		4,
		5
	]
}

While I don't know what exactly you try to do, I hope that looking those examples will help you to achieve your target.

Andreas.

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.