1. Pbuser12 Ateikon
  2. PowerBuilder
  3. Monday, 2 October 2023 14:55 PM UTC

Dear Bruce ,

 

 How can i do a JsonParser to these JsonString morethan one array ..

 

[{"ID":101,"FirstName":"Li"},{"ID":102,"FirstName":null},{"ID":103,"FirstName":"Li"},{"ID":104,"FirstName":null},{"ID":105,"FirstName":"Li"},{"ID":106,"FirstName":null},{"ID":107,"FirstName":"Li"},{"ID":108,"FirstName":null},{"ID":109,"FirstName":"Li"},{"ID":110,"FirstName":null},{"ID":111,"FirstName":"Li"}, {"ID":112,"FirstName":[{"Mode":"email","value":"john@hotmail.com"},{"Mode":"phone","value":[{"Mode":"fax","value":"11111111"},{"Mode":"fax2","value":"2222222"}]}]}, {"ID":113,"FirstName":"Li"},{"ID":114,"FirstName":null},{"ID":115,"FirstName":"Li"}, {"ID":116,"FirstName":[{"Mode":"email","value":"john@example.com"},{"Mode":"phone","value":"555-123-4567"}]}]..Pls help me .

Logan Liu @Appeon Accepted Answer Pending Moderation
  1. Wednesday, 4 October 2023 06:21 AM UTC
  2. PowerBuilder
  3. # 1

Hi,

Please try the GeItemArray method:

String ls_jsonString 
String ls_value
String ls_key
String ls_firstname
Long ll_RootObject
Long ll_rowitem
Long ll_count
Long ll_array
Long ll_arraycount
Long ll_arrayitem
Int ll_rowindex
Int ll_arrayindex
Int li_id
JSONParser jsonParser

// JSON
ls_jsonString = mle_json.Text

jsonParser = create JSONParser
jsonParser.LoadString(ls_jsonString)

ll_RootObject = jsonParser.GetRootItem()
ll_count = jsonParser.GetChildCount(ll_RootObject)

// Each object
For  ll_rowindex = 1 to ll_count
   	ll_rowitem = jsonParser.GetChildItem(ll_RootObject, ll_rowindex)
	
	// Each item
	li_id = jsonParser.GetItemNumber(ll_rowitem, "ID")
	
	ls_firstname = ""
	ls_key = "FirstName"
	Choose Case  jsonParser.GetItemType(ll_rowitem, ls_key)
		case JsonStringItem!
			ls_firstname = jsonParser.GetItemString(ll_rowitem, ls_key)
		case JsonNullItem!
			ls_firstname = 'Null'
		Case  JsonArrayItem!
			// Parse FirstName Array
			ls_firstname = " "
			
			ll_array  = jsonParser.GetItemArray( ll_rowitem, ls_key )
			ll_arraycount = jsonParser.GetChildCount(ll_array)
			For ll_arrayindex = 1 to ll_arraycount
				ll_arrayitem = jsonParser.GetChildItem(ll_array, ll_arrayindex)
				
				Choose Case jsonParser.GetItemType(ll_arrayitem, "value")
					Case JsonStringItem!
						 messageBox("", "Mode: " + jsonParser.GetItemString(ll_arrayitem, "mode") + " " + &
				 			" Value: " + jsonParser.GetItemString(ll_arrayitem, "value"))
					Case  JsonArrayItem!
						// TODO: Parse Value Array	
						// ...
				End Choose

			Next
	End Choose		
	
	MessageBox("Row - ID: " + String(li_id), "FN: " + ls_firstname)
Next

Regards, Logan

Comment
  1. Pbuser12 Ateikon
  2. Tuesday, 10 October 2023 14:36 PM UTC
Thank You Logan for helping me
  1. Helpful
There are no comments made yet.
Pbuser12 Ateikon Accepted Answer Pending Moderation
  1. Tuesday, 10 October 2023 14:57 PM UTC
  2. PowerBuilder
  3. # 2

Thank You for replying..Is this code is correct or not.Iam the fresher of the power builder..Please check ..Iam inserting the JsonData into Data window..

String ls_jsonString
String ls_value
String ls_key
String ls_firstname
Long ll_RootObject
Long ll_rowitem
Long ll_count
Long ll_array
Long ll_arraycount
Long ll_arrayitem
Int ll_rowindex
Int ll_arrayindex
Int li_id,ll_row
JSONParser jsonParser

// JSON
ls_jsonString = '[{"ID":101,"FirstName":"Li"},{"ID":102,"FirstName":null},{"ID":103,"FirstName":"Li"},{"ID":104,"FirstName":null},{"ID":105,"FirstName":"Li"},{"ID":106,"FirstName":null},{"ID":107,"FirstName":"Li"},{"ID":108,"FirstName":null},{"ID":109,"FirstName":"Li"},{"ID":110,"FirstName":null},{"ID":111,"FirstName":"Li"}, {"ID":112,"FirstName":[{"Mode":"email","value":"john@hotmail.com"},{"Mode":"phone","value":[{"Mode":"fax","value":"11111111"},{"Mode":"fax2","value":"2222222"}]}]}, {"ID":113,"FirstName":"Li"},{"ID":114,"FirstName":null},{"ID":115,"FirstName":"Li"}, {"ID":116,"FirstName":[{"Mode":"email","value":"john@example.com"},{"Mode":"phone","value":"555-123-4567"}]}]'

jsonParser = create JSONParser
jsonParser.LoadString(ls_jsonString)

ll_RootObject = jsonParser.GetRootItem()
ll_count = jsonParser.GetChildCount(ll_RootObject)

// Each object
For ll_rowindex = 1 to ll_count
ll_rowitem = jsonParser.GetChildItem(ll_RootObject, ll_rowindex)

// Each item
li_id = jsonParser.GetItemNumber(ll_rowitem, "ID")

ls_firstname = ""
ls_key = "FirstName"
Choose Case jsonParser.GetItemType(ll_rowitem, ls_key)
case JsonStringItem!
ls_firstname = jsonParser.GetItemString(ll_rowitem, ls_key)
case JsonNullItem!
ls_firstname = 'Null'
Case JsonArrayItem!
// Parse FirstName Array
ls_firstname = " "

ll_array = jsonParser.GetItemArray( ll_rowitem, ls_key )
ll_arraycount = jsonParser.GetChildCount(ll_array)
For ll_arrayindex = 1 to ll_arraycount

ll_row = dw_1.insertrow(0)
ll_arrayitem = jsonParser.GetChildItem(ll_array, ll_arrayindex)

Choose Case jsonParser.GetItemType(ll_arrayitem, "value")
Case JsonStringItem!
messageBox("", "Mode: " + jsonParser.GetItemString(ll_arrayitem, "mode") + " " + &
" Value: " + jsonParser.GetItemString(ll_arrayitem, "value"))
Case JsonArrayItem!
// TODO: Parse Value Array
// ...
End Choose

//Insert the data into data window
dw_2.setitem(ll_row, 'id', jsonParser.GetItemNumber(ll_arrayitem, "id"))
dw_2.setitem(ll_row, 'FirstName', jsonParser.GetItemString(ll_arrayitem, "FirstName"))
dw_2.setitem(ll_row, 'Mode', jsonParser.GetItemNumber(ll_arrayitem, "Mode"))
dw_2.setitem(ll_row, 'value', jsonParser.GetItemstring(ll_arrayitem, "value"))

Next
End Choose

MessageBox("Row - ID: " + String(li_id), "FN: " + ls_firstname)
Next

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.