1. Daniel Ferrreira
  2. PowerBuilder
  3. Saturday, 11 July 2020 05:35 AM UTC

Hi guys,

I'm having a hard time parsing the json string below. Can you please help? This is a simplified version:

{
    "meta": {
        "limit": 20,
    },
    "objects": [
        {
            "cat_pai": null,
            "desc": "Cat Test Desc 1",
            "id": 5315423,
            "name": "Cat Nome Teste 1"
        }
    ]
}

//My code

String ls_ret, ls_meta, ls_obj
Long ll_ret, ll_root, ll_limit, ll_tot, ll_obj, ll_id

JSONPackage ljpkg
JSONParser ljp

String ls_j = '{"meta": {"limit": 20},"objects": [{"cat_pai": null,"desc": "Cat Test Desc 1","id": 5315423,"name": "Cat Nome Teste 1"}]}'

ljpkg = Create JSONPackage
ljp = Create JSONParser

//I was able to use JSONPackage and get the values from "meta".
ls_ret = ljpkg.LoadString(ls_j)
ls_meta = ljpkg.GetValue('meta')
ls_ret = ljp.LoadString(ls_meta)
ll_root = ljp.GetRootItem()
ll_limit = ljp.GetItemNumber(ll_root, 'limit')

//Well, then I wasn't able to parse the "objects" array. I tried using this code below, but I can't get the "id" value, it always returns null. Can you please help?

ls_obj = '{"objects": ' + ljpkg.GetValue('objects') + '}' //weird I had to do this concat, is it really necessary? Otherwise the GetChildCount would return 2, and I couldn't find a way to use GetItemArray
ls_ret = ljp.LoadString(ls_obj)
ll_root = ljp.GetRootItem()
ll_tot = ljp.GetChildCount(ll_root) //Seems to be working
If ll_tot = 0 Then Return
ll_obj = ljp.GetItemArray(ll_root, 'objects') //I expected ll_obj would return 1, but it returns 2. Why?
ll_id = ljp.GetItemNumber(ll_obj, 'id') //this always returns null :(

Thanks a lot

Accepted Answer
René Ullrich Accepted Answer Pending Moderation
  1. Monday, 13 July 2020 06:57 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi Daniel,

 

you don't need the JSONParser.

Try this:

 

ljp = Create JSONParser

ljp.LoadString(ls_j)
ll_RootItem = ljp.GetRootItem()
ll_metaitem = ljp.GetItemObject (ll_RootItem, "meta")
MessageBox ("limit", ljp.GetItemNumber (ll_metaitem, "limit"))


ll_objectsitem = ljp.GetItemArray (ll_RootItem, "objects")


ll_ChildCount = ljp.GetChildCount(ll_objectsitem)

// Get array item in a loop
for ll_Index = 1 to ll_ChildCount
// Get array item
ll_ObjectItem = ljp.GetChildItem(ll_objectsitem, ll_Index)

// Array item is JsonObjectItem!
if ljp.GetItemType(ll_ObjectItem) = JsonObjectItem! then
MessageBox ("id", ljp.GetItemNumber(ll_ObjectItem, "id"))
end if

next

 

HTH,

René

Comment
  1. Kevin Ridley
  2. Monday, 13 July 2020 15:55 PM UTC
You could also put the objects array into a datastore or datawindow using ImportJson as long as it's a flat array (no nested arrays).
  1. Helpful
There are no comments made yet.
Daniel Ferrreira Accepted Answer Pending Moderation
  1. Monday, 13 July 2020 14:02 PM UTC
  2. PowerBuilder
  3. # 1

Thanks a lot, Rene!

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.