Json and list

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
kno_dk
Posts: 102
Joined: Tue Feb 23, 2016 12:00 am

Json and list

Post by kno_dk » Tue Nov 05, 2019 10:30 pm

Hi
I have created a wam which create a JSON respons (see below) I have tested the wam in POSTMAn and it Works fine.
I have then created a servermodule and a VL-web page to test the Wam. Here are the code from my Servermodule:
#HttpRequest.Dopost Url(#Url)
If (#HttpRequest.Response.IsSuccessfulRequest)
* Feed the HTTP Response to the JsonReader object
#Reader.SetSourceHttpResponse Httpresponse(#HttpRequest.Response)
* Check if we get a positive response from the server
If (#HttpRequest.Response.IsSuccessHttpStatusCode)
* Get the result (translated) text
#TranslatedText := #Reader.ReadStringWithPath( 'statuscode' )
#RequestOK := True
#Reader.BeginArrayWithPath Path("options_list") Found(#OptionerfundetOk)
#LoopCount := #Reader.GetChildCount
Begin_Loop Using(#STD_NUM) To(#LoopCount)
#Reader.BeginObjectAtIndex Index(#STD_NUM)
#SEOOPT := #Reader.ReadNumberWithName( 'option' )
#SEOJN := #Reader.ReadNumberWithName( 'option_value' )
Add_Entry To_List(#opt_list)
End_Loop

I works fine. The LoopCount is set to 60, as which is the number of entries in the Options_list, But when I debug the servermodul it is alway the values of entry number 1 i get in my opt_list (60 entries).

What am I missing? Is it the JSON from the WAM that is defined wrong or??

Here is the JSON answer:

{"statuscode":"000000", "statustext":"STATUS OK", "options_list": [

{ "option": "01", "option_value": "1" } ,

{ "option": "02", "option_value": "1" } ,

{ "option": "03", "option_value": "1" } ,

{ "option": "04", "option_value": "1" } ,

{ "option": "05", "option_value": "1" } ,

{ "option": "06", "option_value": "1" } ,

{ "option": "07", "option_value": "1" } ,

{ "option": "08", "option_value": "0" } ,

{ "option": "09", "option_value": "0" } ,

{ "option": "10", "option_value": "0" } ,

{ "option": "11", "option_value": "0" } ,

{ "option": "12", "option_value": "0" } ,

{ "option": "13", "option_value": "0" } ,

{ "option": "14", "option_value": "0" } ,

{ "option": "15", "option_value": "0" } ,

{ "option": "16", "option_value": "0" } ,

{ "option": "17", "option_value": "0" } ,

{ "option": "18", "option_value": "0" } ,

{ "option": "19", "option_value": "0" } ,

{ "option": "20", "option_value": "0" } ,

{ "option": "21", "option_value": "1" } ,

{ "option": "22", "option_value": "1" } ,

{ "option": "23", "option_value": "1" } ,

{ "option": "24", "option_value": "0" } ,

{ "option": "25", "option_value": "0" } ,

{ "option": "26", "option_value": "0" } ,

{ "option": "27", "option_value": "0" } ,

{ "option": "28", "option_value": "0" } ,

{ "option": "29", "option_value": "0" } ,

{ "option": "30", "option_value": "0" } ,

{ "option": "31", "option_value": "0" } ,

{ "option": "32", "option_value": "0" } ,

{ "option": "33", "option_value": "1" } ,

{ "option": "34", "option_value": "0" } ,

{ "option": "35", "option_value": "0" } ,

{ "option": "36", "option_value": "1" } ,

{ "option": "37", "option_value": "1" } ,

{ "option": "40", "option_value": "1" } ,

{ "option": "41", "option_value": "1" } ,

{ "option": "42", "option_value": "1" } ,

{ "option": "50", "option_value": "0" } ,

{ "option": "65", "option_value": "0" } ,

{ "option": "71", "option_value": "1" } ,

{ "option": "72", "option_value": "1" } ,

{ "option": "73", "option_value": "0" } ,

{ "option": "74", "option_value": "0" } ,

{ "option": "75", "option_value": "1" } ,

{ "option": "76", "option_value": "1" } ,

{ "option": "77", "option_value": "1" } ,

{ "option": "80", "option_value": "1" } ,

{ "option": "81", "option_value": "0" } ,

{ "option": "82", "option_value": "0" } ,

{ "option": "83", "option_value": "1" } ,

{ "option": "84", "option_value": "0" } ,

{ "option": "93", "option_value": "1" } ,

{ "option": "94", "option_value": "1" } ,

{ "option": "96", "option_value": "1" } ,

{ "option": "97", "option_value": "1" } ,

{ "option": "98", "option_value": "1" } ,

{ "option": "99", "option_value": "1" } ] }

mwilliams
Posts: 12
Joined: Sat May 28, 2016 1:45 am

Re: Json and list

Post by mwilliams » Wed Nov 06, 2019 2:58 am

It looks like you may need to add #Reader.EndObject after the Add_Entry. I found an example in server module XFTSRVM. It also looks like you may want to add #Reader.EndArray after you are done with the loop.

Code: Select all

#Reader.BeginArrayWithPath Path("scheduledFlights") Found(#xFlightTrackerStatus)

If (#xFlightTrackerStatus.IsFalse)

#xFlightTrackerStatus #Result := False

#xFlightTrackerMessage := 'Cannot find scheduled flights'

Return

Else

#Result := True

Endif

* Read all scheduled Flight objects

#LoopCount := #Reader.GetChildCount

Begin_Loop Using(#STD_NUM) To(#LoopCount)

#Reader.BeginObjectAtIndex Index(#STD_NUM)

#xFlightTrackerCarrier := #Reader.ReadStringWithName( 'carrierFsCode' )

#xFlightTrackerFlightNumber := #Reader.ReadStringWithName( 'flightNumber' )

#xFlightTrackerDepatureTime := #Reader.ReadStringWithName( 'departureTime' )

#xFlightTrackerArrivalTime := #Reader.ReadStringWithName( 'arrivalTime' )

#xFlightTrackerDepartureTerminal := #Reader.ReadStringWithName( 'departureTerminal' )

#xFlightTrackerArrivalTerminal := #Reader.ReadStringWithName( 'arrivalTerminal' )

#xFlightTrackerCarrierCode := #Reader.ReadStringWithName( 'carrierFsCode' )

#xFlightTrackerAircraftCode := #Reader.ReadStringWithName( 'flightEquipmentIataCode' )

#xFlightCodeShare := #Reader.ReadStringWithName( 'isCodeshare' )

If (#xFlightCodeShare.UpperCase = 'FALSE')

Add_Entry To_List(#xFlightDetails)

Endif

#Reader.EndObject

End_Loop

#Reader.EndArray

kno_dk
Posts: 102
Joined: Tue Feb 23, 2016 12:00 am

Re: Json and list

Post by kno_dk » Wed Nov 06, 2019 4:29 am

Hi.

Thanks.

Reader.EndObject did the job.

/klaus

Post Reply