JSON parser for the 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
sotos
Posts: 31
Joined: Fri Feb 09, 2018 11:25 pm

JSON parser for the web

Post by sotos »

Hello everyone,

Is there a JSON parser for the web?

I am aware of XPRIM_JsonObject and XPRIM_JsonArray but I would like to keep the JSON on the client side instead of sending it to the server.

best,
Sotiris
User avatar
Dino
Posts: 472
Joined: Fri Jul 19, 2019 7:49 am
Location: Robbinsville, NC
Contact:

Re: JSON parser for the web

Post by Dino »

You can use #PRIM_WEB.HttpRequest and #PRIM_WEB.Json

For example to read this json page:
primwebjson0.png
primwebjson0.png (7.24 KiB) Viewed 5946 times
you can use this code:
primwebjson1.png
primwebjson1.png (70.17 KiB) Viewed 5946 times

Code: Select all

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

Define_Com Class(#PRIM_MD.Label) Name(#Text1) Caption('Title') Displayposition(1) Left(32) Parent(#COM_OWNER) Tabposition(1) Top(30) Width(305)
Define_Com Class(#PRIM_MD.Label) Name(#Text2) Caption('UserId') Displayposition(2) Left(32) Parent(#COM_OWNER) Tabposition(2) Top(80) Width(305)
Define_Com Class(#PRIM_MD.Label) Name(#Text3) Caption('id') Displayposition(3) Left(32) Parent(#COM_OWNER) Tabposition(3) Top(130) Width(305)
Define_Com Class(#PRIM_MD.Label) Name(#Text4) Caption('Completed') Displayposition(4) Left(32) Parent(#COM_OWNER) Tabposition(4) Top(180) Width(305)

Evtroutine Handling(#Com_owner.Initialize)
#COM_OWNER.GetJsonResponse( '/todos/1' )
Endroutine

Mthroutine Name(GetJsonResponse) Access(*PRIVATE)
Define_Map For(*INPUT) Class(#Prim_Str) Name(#Resource)

Define_Com Class(#PRIM_WEB.HttpRequest) Name(#Request)
Define_Com Class(#PRIM_WEB.Json) Name(#Json)

#Request.Url := "http://jsonplaceholder.typicode.com" + #Resource
#Request.ExecuteAsync

Evtroutine Handling(#Request.Completed)
If (#Request.Response *IsNot *null)
Case (#Request.Response.StatusCode)
When (= 200)
#Json := #Request.Response.Content
#Com_owner.Update( #Json.RootItem )

Otherwise
#Com_owner.UpdateForFailure( #Request )
Endcase
Endif
Endroutine
Endroutine

Mthroutine Name(Update) Access(*PRIVATE)
Define_Map For(*INPUT) Class(#prim_web.JsonElement) Name(#JSONResult) Pass(*BY_REFERENCE)

#Text1.Caption := #JSONResult.Item<"title">.AsString
#Text2.Caption := #JSONResult.Item<"userId">.AsString
#Text3.Caption := #JSONResult.Item<"id">.AsString
#Text4.Caption := #JSONResult.Item<"completed">.AsString
Endroutine

Mthroutine Name(UpdateForFailure) Access(*PRIVATE)
Define_Map For(*INPUT) Class(#Prim_web.httprequest) Name(#Request) Pass(*BY_REFERENCE)
Endroutine
End_Com
to get this result:
primwebjson2.png
primwebjson2.png (8.02 KiB) Viewed 5946 times
if you want to handle an array there as well, you can use the rootItem and #prim_web.JsonElement :

Code: Select all

#Com_owner.ProcessAnArray( #Json.RootItem )
(...)

Mthroutine Name(ProcessAnArray) Access(*PRIVATE)
Define_Map For(*INPUT) Class(#prim_web.JsonElement) Name(#JSONResult) Pass(*BY_REFERENCE)
* Lets assume I have a #list1 with fields #std_text, #std_textl, #std_qord, etc.

* In this example the answer is just an array
For Each(#ArrayElem) In(#JSONResult)
For Each(#Entry) In(#ArrayElem)
Case (#Entry.key)
When Value_Is(= "lat")
#std_text := #Entry.AsString
When Value_Is(= "lon")
#std_textl := #Entry.AsString
When Value_Is(= "display_name")
#std_qord := #Entry.AsString
Endcase
Endfor
add_entry #Mylist
Endfor

Endroutine
Post Reply