Reading json in VL Web

This Q&A forum allows users to post and respond to "How Do I Do ....." questions. Please do not use to report (suspected) errors - you must use your regional help desk for this. The information contained in this forum has not been validated by LANSA and, as such, LANSA cannot guarantee the accuracy of the information.
Post Reply
soa
Posts: 317
Joined: Mon Dec 07, 2015 3:15 pm

Reading json in VL Web

Post by soa » Fri Nov 29, 2019 3:46 pm

I need to read a json object in a VLWEB application.

XPRIM_RandomAccessJsonReader is not available on the web

This forum makes mention of #PRIM_JsonReader as native VLWEB component but my IDE tells me it can't be found.

How should I proceed?

dannyoorburg
Posts: 169
Joined: Mon Jan 04, 2016 9:50 am
Location: Australia

Re: Reading json in VL Web

Post by dannyoorburg » Fri Nov 29, 2019 4:03 pm

Hi,

VL WEB client side has the PRIM_WEB.JSON object/classes.

https://docs.lansa.com/14/en/lansa016/prim_web.json.htm

Hope that helps,
Danny

soa
Posts: 317
Joined: Mon Dec 07, 2015 3:15 pm

Re: Reading json in VL Web

Post by soa » Fri Nov 29, 2019 4:40 pm

Big help Danny, thanks. Can you point me in the direction of some examples of use

I need to extract values from a json string like the following.

{
"eformlist":[
{
"Type":"SHWEX",
"Code":"ONSTG",
"EFID":"47",
"EFIID":"4707"
},
{
"Type":"SHWEX",
"Code":"ONSTI",
"EFID":"48",
"EFIID":"566"
}
]
}

dannyoorburg
Posts: 169
Joined: Mon Jan 04, 2016 9:50 am
Location: Australia

Re: Reading json in VL Web

Post by dannyoorburg » Fri Nov 29, 2019 5:24 pm

There's many ways to navigate the JSON, depends whether you want a particular value, or iterate arrays...

I just put a simple sample together, hope this helps.

Danny

Code: Select all

Begin_Com Role(*EXTENDS #PRIM_WEB) Theme(#SYS_THEME<MaterialDesignBlue>)

Define_Com Class(#PRIM_MD.RaisedButton) Name(#Button) Caption('PRIMARY') DisplayPosition(1) Left(46) Parent(#COM_OWNER) TabPosition(1) ThemeDrawStyle('MediumTitle') Top(26)

Evtroutine Handling(#Button.Click)
Define_Com Class(#PRIM_WEB.JSON) Name(#JSON)
Define_Com Class(#PRIM_WEB.JsonElement) Name(#FormList) Reference(*DYNAMIC)

#JSON := '{"eformlist":[{"Type":"SHWEX","Code":"ONSTG","EFID":"47","EFIID":"4707"},{"Type":"SHWEX","Code":"ONSTI","EFID":"48","EFIID":"566"}]}'

#FormList <= #JSON.RootItem<"eformlist">

For Each(#FormListItem) In(#FormList)

#SYS_WEB.Alert( ("Type: " + #FormListItem<"Type">.AsString + ", EFID: " + #FormListItem<"EFID">.AsString) )

Endfor

Endroutine

End_Com

soa
Posts: 317
Joined: Mon Dec 07, 2015 3:15 pm

Re: Reading json in VL Web

Post by soa » Mon Dec 02, 2019 11:52 am

Worked out of the box, exactly what I wanted. Thanks Danny. I'll share this with the team.

Post Reply