|
|
|
|
|
- Overview
- JSON and WEBDEV
- Getting information in JSON format
- Usage example of the JSONExecute function
- Usage example of the JSONExecuteExternal function
- Creating pages that return JSON data
JSON (JavaScript Object Notation) is a light format for exchanging data. Based on JavaScript, JSON is a text format independent of any other language. JSON is based on two structures: - a set of name/value couples, interpreted in WLanguage by structures.
- a list of organized values, interpreted in WLangage by arrays.
WEBDEV is used to: - get information in JSON format in a WEBDEV site.
- create WEBDEV pages that return information in JSON format. You have the ability to create a site that provides JSON services (a site for monitoring packages for example).
Getting information in JSON format To get information in JSON format, WEBDEV allows you to run a page that returns information in JSON format. Two WLangage functions are available: | | JSONExecute | Calls a server URL of the same domain that returns data in JSON format (JavaScript Object Notation). | JSONExecuteExternal | Calls an external server URL that returns data in JSON format (JavaScript Object Notation). The data is processed in a specific procedure. |
These functions are browser functions. The data received is processed by a browser code. Usage example of the JSONExecute function The following code is used to run an AWP page in order to get the list of contacts in JSON format. The steps are: - Declaring a dynamic object. This dynamic object will contain the result in JSON format.
MyContacts is dynamic object
- Calling JSONExecute:
MyContacts = JSONExecute(FolderWeb()+"FR/PAGE_Object.awp?id=12")
- Processing the JSON data: This process is performed in browser code.
Let's take a simple example: the JSON data is returned in the following format:
{id: 12, list: [ {lastname: "smith", firstname: "john"}, {lastname: "doe", firstname: "mary"}, {lastname: "martin", firstname: "laura"}] }
You can for example:- retrieve the value of a member of the dynamic object. For example:
- retrieve the different elements from a list of values. For example:
FOR i = 1 _TO_ Dimension(MyContacts:list) ListAdd(LIST_List_of_contacts, ... MyContacts:list[i]:lastname + " Â " + ... MyContacts:list[i]:firstname) END
Usage example of the JSONExecuteExternal function In this case, the page used to retrieve the JSON data is not found in the same domain. The security rules of Internet impose to use a callback function to process the result in asynchronous way. Therefore, JSONExecuteExternal allows you to specify the name of the procedure run to process the JSON data. Example: In this example, an Active WEBDEV Page is called and the data is processed in the FunctionResponse procedure: JSONExecuteExternal("http://MySite/MySite_WEB/US/PAGE_Object.awp?id=12", ... "JsonCallback", FunctionResponse)
Creating pages that return JSON data WEBDEV gives you the ability to create a site that provides JSON services. The pages used to get the JSON data must return a character string of a specific format. These pages can be AWP pages or PHP pages. Case of a page called by JSONExecute: The following code is used to return an identifier and a list of values: sObject is string = [ {id: 12, list: Â [ {lastname: "smith", firstname: "john"}, {lastname: "doe", firstname: "mary"}, {lastname: "martin", firstname: "laura"}] } ] // StringToUTF8 is used to manage the accented characters StringDisplay(StringToUTF8(sObject))
StringDisplay is used to return the JSON information. StringToUTF8 is used to return a string in UTF8 format. This last function is mandatory to get a valid format. Case of a page called by JSONExecuteExternal: In addition to the code used to manage the JSON element to return, the name of the procedure that will be used to process the data must be specified in the data returned. The name of this procedure is indicated in parameter on the URL of the page. The following example is used to manage the call with JSONExecute and JSONExecuteExternal. For an external call, you must check the presence of a specific parameter found on the URL. This parameter was supplied to the user of the JSON service. sObject is string = [ {id: 12, list: Â [ {lastname: "smith", firstname: "john"}, {lastname: "doe", firstname: "mary"}, {lastname: "martin", firstname: "laura"}] } ] // Manage an external call. // Checks the presence of a procedure name on the URL IF PageParameter("JsonCallback") <> "" THEN sObject = PageParameter("JsonCallback") + "("+ sObject + ");" END Â // StringToUTF8 is used to manage the accented characters StringDisplay(StringToUTF8(sObject))
In order for the users of WEBDEV sites to be able use the JSON services, all you have to do is inform these users of the parameters to specify in the address of the page to start. Remark: See a specific documentation for more details.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|