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
JSON parser for the web
Re: JSON parser for the web
You can use #PRIM_WEB.HttpRequest and #PRIM_WEB.Json
For example to read this json page: you can use this code:
to get this result:
if you want to handle an array there as well, you can use the rootItem and #prim_web.JsonElement :
For example to read this json page: you can use this code:
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
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