1. Pbuser12 Ateikon
  2. PowerBuilder
  3. Tuesday, 10 October 2023 15:00 PM UTC

Dear Team,

 

 

Iam inserting the JsonData into the Datawindow...Iam unable to resolve this ..Please help me..

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

Pbuser12 Ateikon Accepted Answer Pending Moderation
  1. Tuesday, 17 October 2023 07:43 AM UTC
  2. PowerBuilder
  3. # 1

Thanks Kevin 

Comment
There are no comments made yet.
Pbuser12 Ateikon Accepted Answer Pending Moderation
  1. Wednesday, 11 October 2023 07:21 AM UTC
  2. PowerBuilder
  3. # 2

Thanks Kevin for reply me.Really iam not understand..my aim is to insert the JsonData into Data Window..Can you Please modify code .I will better understand..

 

 

Comment
  1. Kevin Ridley
  2. Friday, 13 October 2023 14:56 PM UTC
I explained in pretty good detail how to do this. I believe in "teaching someone to fish" instead of fishing for them and giving them your fish. I'm suggesting you should go through the documentation on the JSON parser if you're not sure how to parse the JSON. Aside from that, if you just want someone to write the code for you, you could hire me or one of the other folks from the Independent consultants list here:

https://www.appeon.com/consultants

You can contact me directly on Linked in if you are interested in doing that.
  1. Helpful 3
  1. Miguel Leeuwe
  2. Tuesday, 17 October 2023 08:43 AM UTC
OLE!
  1. Helpful
There are no comments made yet.
Kevin Ridley Accepted Answer Pending Moderation
  1. Tuesday, 10 October 2023 22:49 PM UTC
  2. PowerBuilder
  3. # 3

I have a few comments/suggestions.  First what you have is some hierarchical json data (looks like you forgot to put the firstname value in your last person) consisting of person data (id, firstname) and possible multiple occurrences of contact data (mode, value).  Datawindows are basically flat rows of data, so it's a challenge to flatten out the hierarchical data.  It can be done though.  You will need create a datawindow with all of the data items in each row.  For each occurrence of the contact data, you will need to duplicate the current person data in a new row.  So, for example, person 112 has 4 occurrences of mode, so you will need 4 total rows of person 112 (with different mode data in each row).  Persons with 1 or no mode will only have 1 row total in the dw.  So you get your person data, insert a new row, add the person data, then check for modes.  If there's none, go to the next person, if there's 1, just add the mode data.  If there's multiple, add a new row for each additional mode using the current person.  That should get you what you need.  Now 1 more suggestion, to make it look slick, make it a Treeview DW!

 

Kevin

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.